Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract identifiers as a string containing the word "the" from an HTML file

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().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading