0

I'm new to javascript and web development so this is a pretty basic question (although I've searched around). I need to access a table which contains many columns one of which contains rows of people's names. In each row the id for the text which holds the person's name is setup like this

<span class="PSLONGEDITBOX" id = "MTG_INSTR$2"> JOHN DOE </span> == $0

in the next row it's it would be like this

<span class="PSLONGEDITBOX" id = "MTG_INSTR$3"> BOB DAVID </span> == $0

So in order to iterate through all elements which contain names, is there anyway I could simply iterate over all ids which contain "MTG_INSTR"? If not, how would you approach it?

Also have no idea what that == $0 at the end does

Sentinel
  • 441
  • 1
  • 6
  • 25
  • Possible duplicate of [Find all elements whose id begins with a common string](http://stackoverflow.com/questions/10111668/find-all-elements-whose-id-begins-with-a-common-string) – Liam May 12 '17 at 12:26
  • Or better yest [Get element by part of Name or ID](http://stackoverflow.com/questions/15874630/js-get-element-by-part-of-name-or-id) – Liam May 12 '17 at 12:27

1 Answers1

1

Try using document.querySelectorAll with 'span[id^="MTG_INSTR"]' selector:

var els = document.querySelectorAll('span[id^="MTG_INSTR"]');

console.log(els);
els.forEach(x => console.log(x.innerText));
<span class="PSLONGEDITBOX" id = "MTG_INSTR$2"> JOHN DOE </span>
<span class="PSLONGEDITBOX" id = "MTG_INSTR$3"> JOHN DOE </span>
<span class="PSLONGEDITBOX" id = "NMTG_INSTR$2"> JOHN DOE </span>

Remember you can also achieve the same result selecting by the class:

var els = document.getElementsByClassName("PSLONGEDITBOX");

console.log(els);

var els = document.querySelectorAll(".PSLONGEDITBOX");

console.log(els);
<span class="PSLONGEDITBOX" id = "MTG_INSTR$2"> JOHN DOE </span>
<span class="PSLONGEDITBOX" id = "MTG_INSTR$3"> JOHN DOE </span>
<span class="PSLONGEDITBOX" id = "NMTG_INSTR$2"> JOHN DOE </span>
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • Doesn't work, I think it's because the text is deep inside a table? I also tried getting just a single person's name using 'span[id="MTG_INSTR$1"]' but still els appears to be empty (length 0). I'm pretty sure the value of document is correct since if I evaluate it in the console it shows me the DOM of the web page (actually spent all of yesterday getting that part to work). – Sentinel May 12 '17 at 21:27