Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTML String with jQuery

Tags:

html

jquery

Is it possible to use a jQuery selector on an HTML string? I'm trying to get the html contents of the div, foo.

var code = "<div id='foo'>1</div><div id='bar'>2</div>";
alert( $(code).html( ) ); // returns 1 (not sure how...)

How can I get the html contents of foo and of bar in two different statements?


1 Answers

var code = $("<div id='foo'>1</div><div id='bar'>2</div>");

code.each(function() {
    alert( $(this).html() );
})
like image 154
meder omuraliev Avatar answered Aug 08 '26 12:08

meder omuraliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!