How would loop through HTML stored in a variable for ‘ul’ tags? This is what I have so far but the alert() don’t even trigger, Im a bit lost…
var data = 'HTML';
$('ul', data).each(function(i,v) {
var theTag = v.tagName;
var theElement = $(v);
var theValue = theElement.html();
alert(theTag+'\n'+theElement+'\n'+theValue);
});
Thanks
You’re trying to use the “selector, context” form of the
$()call:A string of HTML doesn’t match any of the things that
contextis supposed to be so jQuery doesn’t know what to do with your arguments and makes the wrong guess.You probably want to do it like this:
Demo: http://jsfiddle.net/ambiguous/gxGB8/
Or, if you don‘t know at what level the
<ul>elements will be, wrap the HTML in a<div>and usefindinstead offilter:Demo: http://jsfiddle.net/ambiguous/tM4ua/