I have html on a variable, how can I do a select on it and then an each on this variable?
Example:
var ht = '<div id="1" class="pp">Hi</div><div id="2">Hola</div><div id="3" class="pp"> Bonjour</div>';
$(ht).find(".pp").each( function(i){
var i = this.id;
console.log(i);
});
of course this does not work.
Thanks!
The problem with your code is that you have several sibling
.ppelements that don’t have a parent element. When you create a jQuery object with them, it creates a selection containing each of the.ppelements.When you use
find, it looks at descendant elements. The elements in the selection itself are not tested. You need to usefilterinstead, which tests the elements that are actually selected, not their descendants: