tags:

views:

47

answers:

2

Hi there, I'm using the following code:

  $.each($dataObj, function(index, value) {
    $(index).html(value); 
  });

Which of course does not work since $(index) is not a valid syntax.

Each index in the object corresponds to a a unique div-id on the webpage, so what I want to do is to replace the html in all of the div-id's listed in the dataObj with 'value'. How do I do that?

+1  A: 

To make it valid jQuery syntax, simply add the 'ID' selector in front of it:

$.each($dataObj, function(index, value) {
  $('#' + index).html(value); 
});
Peter J
Exactly, thank you.
soren.qvist
+1  A: 

You can use the $dataObj to access each.

Depending on it's contents, you might want:

$dataObj.eq(index).html(value);

However, it seems like you also might want to do an each loop like so:

$dataObj.each(function(i, value){
  $(this).html(value);
});

But even that's unnecessary if it's all the same value

$dataObj.html(value);

Would effectively loop through each element for you.

Alex Sexton
I agree with you, this would be the 'proper' way to do it...but there were not enough specifics in the question to define exactly what the $dataObj was.
Peter J