I’m trying to manipulate all my HTML tags this code.
I listed them like so:
var elements = document.getElementsByTagName("*")
And I would like to manipulate the variable
elements
and modify the tags in it.
Thing is, I need to add an ID or some sort of class on all of them.
Is there any way other than having to add an id or classname?
Is there any special id given to the tags by default or something?
Thanks a lot.
EDIT:
I use data-id usage to manipulate the elements. So… now my code looks like this:
const dataIDName = "data-spellchecker"
//Init the data-id
noOfElements++;
elements[i].setAttribute(dataIDName, noOfElements);
//Get the DOM element by data-id variable
//dataId is ur data-id variable
document.querySelector(`[${dataIDName}="${dataId}"]`);
2
Answers
You can assign data-id and a value to it
you can access it in jquery using
example:
You can get the elements mainly based on:
Also, use xpaths with your own logic.