1

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?

2 Answers 2

1

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

$.each($dataObj, function(index, value) {
  $('#' + index).html(value); 
});
0
1

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.

1
  • 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
    Commented Feb 25, 2010 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.