mercredi 11 février 2015

jQuery cannot find class element from some html returned via ajax call


I am running an ajax call that returns some HTML as a string. For the purpose of this question I will call this <div class='abc'>ABC123</div> when I get this back I want to check and see if the class "abc" has a value and what that value is. However when I run a .find() I cannot find the class, I can find the div, but not the specific class. Just using the div is not adequate because in the real live code the HTML is very complex and has many divs and classes. Below is some JS that illustrates my point.



var x = "<div class='abc'></div>";
$(x).hasClass("abc"); // returns true
$(x).find(".abc"); // Returns empty array


Why is it that the first line returns true, but the selector cannot find the element?


Thanks!





Aucun commentaire:

Enregistrer un commentaire