tags:

views:

189

answers:

2

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  A: 

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?

Roatin Marth
+2  A: 
var code = $("<div id='foo'>1</div><div id='bar'>2</div>");

code.each(function() {
    alert( $(this).html() );
})
meder
+1 jquery can take HTML inside $("..."). That's all you need to do.
cletus