scott
scott

Reputation:

Parsing HTML String with 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?

Upvotes: 3

Views: 5926

Answers (2)

meder omuraliev
meder omuraliev

Reputation: 186562

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

code.each(function() {
    alert( $(this).html() );
})

Upvotes: 6

Roatin Marth
Roatin Marth

Reputation: 24085

Try map?

var code = "<div id='foo'>1</div><div id='bar'>2</div>";
var html = $(code).map(function() { return $(this).html() });
html[0]; // "1"
html[1]; // "2"

Is that what you mean?

Upvotes: 2

Related Questions