3

suppose in a variable html is saved like

var vhtm="<div><div id='test'>zebra</div><div id='foo'>here is my many other html content</div></div>";

i want to parse the above html stored in variable with the help of jquery. i need to extract a particular div by id and including its content. the return result would like

<div id='foo'>here is my many other html content</div>

so i need to find div by id like id='foo'

please help me how to parse the above like jquery.

asked Aug 18, 2011 at 8:01

5 Answers 5

5

try something like this :

$(vhtml).find('#foo');
answered Aug 18, 2011 at 8:03
Sign up to request clarification or add additional context in comments.

Comments

4

Try this

var divHtml = $(vhtm).find("#foo").html();
answered Aug 18, 2011 at 8:05

Comments

1

Since you want the output to contain the whole div including content I'd suggest you use some regular expressions :) Like this maybe:

var vhtm="<div><div id='test'>zebra</div><div id='foo'>here is my many other html content</div></div>";
var regex = /<div id='foo'>.*?<\/div>/;
alert(vhtm.match(regex));

Result:

enter image description here

answered Aug 18, 2011 at 8:43

3 Comments

I see your point, but my example still works if he's not stuffing his <div id='foo'> with a truckload of complex code ;)
What about another <div> inside that div? Its not 'truckload of complex code', but it'll still break it.
0

I don't know of any nice way to do that. I would set this as the HTML contents of an DOM element ($(document.createElement('div')).html(vhtm).find('#foo')) or create an about:blank iframe, add the HTML to it and use $(iframe.contentWindow.document.body).find(...) if it requires isolation from the page you're running the script on for some reason

answered Aug 18, 2011 at 8:04

5 Comments

Note that $(document.createElement('div')) is equal to $('<div />').
It does the same thing, but its 35% slower. You seriously down-voted the answer because of that?
Not for that. The solution is overly complicated in this case. The difference seems to be 25 % with the complete code in favour of the pure jquery version. The performance difference here is largely irrelevant though.
It's often about readability, and maintainability - both are valid, but I think, provided you're only doing it one, then the string method is preferable. But +1 for the js performance proof.
I have something along the lines of $.c=function(tagName, attrs, text) { return $(document.createElement(tagName)).attr(attrs||{}).text(text||''); } (without the useless calls to attrs() and text() when there are no arguments, but does the same thing) to make it more readable. I find that even more readable than plain HTML.
0

This should do it:

http://jsfiddle.net/HenryGarle/Vpa3q/

var vhtml="<div><div id='test'>zebra</div><div id='foo'>here is my many other html content</div></div>";
var foo = $(vhtml).find('#foo').html();
alert(foo);
answered Aug 18, 2011 at 8:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.