harish
harish

Reputation: 2483

How to re write innerHTML in jQuery

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.

Upvotes: 4

Views: 541

Answers (6)

xdazz
xdazz

Reputation: 160833

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.

Upvotes: 4

Appasaheb Kapase
Appasaheb Kapase

Reputation: 1

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

Upvotes: -2

Suresh Atta
Suresh Atta

Reputation: 121998

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/

Upvotes: 1

mguimard
mguimard

Reputation: 1891

Try this

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

Upvotes: 1

maqjav
maqjav

Reputation: 2434

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);

Upvotes: 1

bipen
bipen

Reputation: 36531

try this

$(this).html(thehtmlval);

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

Upvotes: 4

Related Questions