4

I have the following in my js.

for (var i = 0; i < this.length; i++) {
this[i].innerHTML = thehtmlval;
}

I want to write the same in jQuery.

I did some search and saw this but dont know how to apply it in this case. Can someone please help.

Community
  • 1
  • 1
harsh
  • 2,399
  • 9
  • 29
  • 47

6 Answers6

4

Assume this in your code is a NodeList:

$(this).each(function() {
  $(this).html(thehtmlval);
});

Or just: $(this).html(thehtmlval); because jQuery already did the loop for you inside.

xdazz
  • 158,678
  • 38
  • 247
  • 274
4

try this

$(this).html(thehtmlval);

if multiple then you can use jquery $.each()

bipen
  • 36,319
  • 9
  • 49
  • 62
1

First we should know what this is.

For example if this is just a div, you could use the next code:

${'#myDivId'}.html(thehtmlval); 
or
${this}.html(thehtmlval); 

If you are trying to add a text, you can use the next one:

${'#myDivId'}.text(thehtmlval); 
or
${this}.text(thehtmlval);
maqjav
  • 2,310
  • 3
  • 23
  • 35
1

Try this

for (var i = 0; i < this.length; i++) {
    $(this[i]).html(thehtmlval);
}
mguimard
  • 1,881
  • 13
  • 14
1

Try html() which ovverides previous html in side the selector

$(selector).each(function() {
  $(selector).html(htmlval);   //ovverides previous html in side the selector  
});

http://api.jquery.com/html/

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
-2

instead of using innerHTML or HTML() use text().