JQuery selects DOM elements and accesses internal elements

I am trying to make a small script. I have a main div that has 300 internal divs and I access them through:

$("#items").find(".item").each(function(index,ele){
    console.log(ele);
})

This way it writes me all the internal divs. Now I want to access the labels and read their internal html of these divs found.

How can i do this? Tried to mess around with ele.labeland ele.$("label"), but it doesn't work.

+4
source share
5 answers

You need to use .findor .childrento find child / nested elements.

$(ele).children('label') // takes just direct children
$(ele).find('label') // take all nested labels
+3
source

Try this and let me know if this helps.

$("#items").find(".item").each((i, e) => console.log(e.innerHTML))
Run codeHide result
+1
source

html(), innerHTML of label.

$("#items").find(".item label").each(function(index,ele){
    console.log($(ele).html())});

$("#items").find(".item").each(function(index,ele){
    console.log($(ele).find('label').html();)})
0

javascript:)

itemslabelArr = document.querySelectorAll("#items .item label")
for (var i = 0, len = itemslabelArr.length; i < len; i++) {
console.log(itemslabelArr[i].innerHTML);
}
0
$("#items .item").each(function(index,element){
    var labelText = $('label', element).text(); // or .html()
    console.log(labelText);
})
0

Source: https://habr.com/ru/post/1691406/


All Articles