I am struggling with a basic question related to HTML structure. Here is the code snippet I am working with:
<ul>
<li>
<ul class=t2 id=15>
<li class='item'>a<span class='val'>b</span></li>
<li class='item'>c<span class='val'>d</span></li>
<li class='item'>e<span class='val'>f</span></li>
<li class='item'>parameters : </li>
<li>
<ul class=t3 id=16>
<li>
<ul class=t4 id=17></ul>
</li>
<li>
<ul class=t4 id=18></ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
I am trying to select all child ul nodes of the ul element with id 16 and retrieve their ids. However, I am only able to select the ul with an id of 17, not its sibling with id 18. The JavaScript code I'm using is as follows:
if (document.getElementById(this.toDelete[i]).getElementsByTagName('ul').length >= 1) {
var tag = document.getElementById(this.toDelete[i]).getElementsByTagName('ul');
for (var k = 0; k < tag.length; k++) {
console.log("tag name: " + tag[k].id + " these will be pushed to Delete");
}
}
The above logic seems flawed as it only retrieves id 17 but not 18. I suspect this is because the script is selecting the ul without an id attribute. How can I modify the code to include both ids in the output?
Any help would be greatly appreciated. Thank you.
UPDATE: Here is the full function I am working with. It involves deleting certain items based on their ids and retrieving the ids of their child ul elements. Please note that the code may need further refinement for optimal functionality.
deleteItems: function () {
for (var i = 0; i < this.toDelete.length; i++) {
console.log("Item to be deleted: " + this.toDelete[i]);
for (var j = 0; j < this.items.length; j++) {
if (this.items[j].id == this.toDelete[i]) {
this.items[j] = ""; //this should be a slice
if (document.getElementById(this.toDelete[i]).getElementsByTagName('ul').length >= 1) {
var tag = document.getElementById(this.toDelete[i]).getElementsByTagName('ul');
for (var k = 0; k < tag.length; k++) {
console.log("tag name: " + tag[k].id + " these will be pushed to Delete");
}
this.toDelete.push(document.getElementById(this.toDelete[i]).getElementsByTagName('ul')[0].id);
//check to see if it has those there sister nodes.
}
}
}
}
},