I have a function that accepts 4 IDs. I need to pass it all IDs as a string, which contain the word "the".
I tried to output all elements using JQuery and this algorithm:
var arr = document.querySelectorAll('[id^="the"]');
for (i = 0; i < arr.length; i ) {
console.log(arr[i]);
}
Outputs items that I can't convert to a string with toString()
and split with splice()
.
CodePudding user response:
Your approach is correct. You were just one step away.
Once you've selected the elements are are looping through them, use the id
property to get the element's ID. You can then split the ID by a space and get the second item to get the part after "the."
var arr = document.querySelectorAll('[id^="the"]');
for (i = 0; i < arr.length; i ) {
const id = arr[i].id.split(" ")[1]
console.log(id)
}
<div id="the one"></div>
<div id="the two"></div>
<div id="the three"></div>