bismuth boss bismuth boss - 3 months ago 8
HTML Question

How to find the id of user specified html tags in pure javascript

I am trying to find the id of user specified html tags. For instance the user could input:

<p id="HiImATag"></p>

or they could input any other tag with any other id. I need a way to find the id of specific html tags (such as getting the id from
<li>
or
<p>
tags without knowing the id in the first place) in pure javascript; however, if it is not possible I could use JQuery. Thank you in advance!

Answer

Array.prototype.forEach.call(document.querySelectorAll("p"), function(el) {
  alert(el.id);
});
<p id="hello"></p>
<p id="world"></p>
<p id="foo"></p>