I have div elements and hold text/string inside them, then I try to iterate them, and text() doesn’t work, but innerHTML work just fine.
var arr = $('div.elm');
$.each(arr, function(i,j){
alert(j.text()); // it's not working
console.log(j.text()); // nothing's in log
alert(j.innerHTML); // works fine!
});
text()is a jQuery method,innerHTMLis an DOM Element attribute.When you call
$.eachon a jQuery object, the second parameter you receive (the element) will be a DOM element, not a jQuery object.text()method is similar to callinginnerText/textContenton a HTML Element.html()method is similar to callinginnerHTMLon a HTML Element.If you want to use jQuery methods on your parameter passed in by
each, you have to wrap it in a jQuery object: