skip to Main Content

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


  1. You can assign data-id and a value to it

    you can access it in jquery using

    const value = $(event.currentTarget).attr('data-id')
    

    example:

    <button class="submitButton" id="btn1" data-id="1">Submit</Button>
    
    Login or Signup to reply.
  2. You can get the elements mainly based on:

    1. Id
    2. Class
    3. Name
    4. Tagname

    Also, use xpaths with your own logic.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search