Removing comments and text nodes from the jQuery collection
$html = $("<!-- comment --> <p>text</p>");
creates such a jQuery collection
$( [the comment], [text node], p )
How can I access only a paragraph? .Find ("p") returns an empty collection
And, for extra points,
$html = $("<p>text</p>");
creates such a jQuery collection
$( p )
Is there a safe way to access p, and only p that works, is there a comment or not?
+4
4 answers
One way is to get by index, as in $html = $("<!-- comment --> <p>text</p>");
, you can get the p-tag using $($html[2])
.
OR
$html = $("<!-- comment --> <p>text</p>"); $target = new Object(); for(key in $html){ if(typeof $html[key] === 'object' && $html[key].localName === 'p') $target = $html[key]; }
0