Aaron Shen
Aaron Shen

Reputation: 8374

What does jQuery html( ) returns?

i have seen the code below which is interesting to me:

var target = $('#target');
target.html(target.html().replace(/h2/g,'h3'));

i'm wondering html( ) return the html content of that element, why it can use .replace( ) method of javascript String?

Upvotes: 0

Views: 518

Answers (3)

nnnnnn
nnnnnn

Reputation: 150030

As explained quite clearly in the documentation, called with no arguments it returns the html content as a string... (What else would html be?)

As an aside, rather than nesting a call to .html() inside another, to do a replacement on the same element(s) you can do this:

$("#target").html(function(i, h) { return h.replace(/h2/g, "h3"); });

Upvotes: 3

Carth
Carth

Reputation: 2343

html can get or set the contents of an object. http://api.jquery.com/html/

The outer call is setting the content using the inner paramaterless call to retrieve the initial html content as a string on which it calls replace.

Upvotes: 0

Mike
Mike

Reputation: 46

It can use the replace method because .html returns html as a string.

See http://api.jquery.com/html/ for more.

Upvotes: 0

Related Questions