Solution 1 :

Yes an ID tag can contain a number. However, refrain from starting it with a number like most naming rules in programming advises. Turns out it doesn’t work when you try to target that ID with CSS.

So go crazy with the numbers as long as it doesn’t start the ID

Solution 2 :

If I’ve understood your question correctly, what is stopping you from looping through all dom elements in the page and then changing their IDs ?

While changing the IDs run an increment counter in parallel so that you can add it to the ID (hint: you can use the loop counter which you will be using to loop through all the dom elements) or else generate a random number instead. To be on the safe side, before you add an ID to a given element make sure that ID doesn’t belong to any other element in the page.

Problem :

I’ve been searching this site and googling everywhere and can’t find an answer so chances are that I’m out of luck and not possible to do…

Anyway, I’m working on a old site that adds elements with IDs. Those elements can be present more than one on the same page… and yes, I know that is bad practice, but that is the way I got it and can’t change it so I have to work with I have.

So my question is if it’s possible to add a number to the ID in the html page, that I can access, to make it so that those IDs be something like name_123, being that the number would change for every new element added into the page?

I suposse I can change the IDs to class but I would still need a way to have an unique identifier…

Any help pointing me in the right direction? Or any other sugestion?

Thank you.


Comment posted by Obsidian Age

An HTML5 ID can contain a number, but should not

Comment posted by user2740650

@Osvaldo Correia I think everyone misinterpreted your question and it was closed as a duplicate. I think maybe you were looking for a way to find and rename IDs. If so you’ll have to ask another question and phrase it so it’s not misinterpreted.

Comment posted by Rob

I got side tracked by the comments, too, and voted to reopen.

Comment posted by Osvaldo Correia

I know all valid values in HTML for ID , both html4 and 5. That was not my question. My question was if there is a way to add a number that would change for every new element that this site will add into the page

Comment posted by Richard

I don’t know man, your question was closed as a duplicate of something you claim not to be asking. Well I hope you find your answers elsewhere

Comment posted by Richard

@OsvaldoCorreia you can’t do that with html alone, you’ll need JavaScript.

Comment posted by Osvaldo Correia

LIke I said in my question, I have been looking everywhere so I was not with high hopes to find solution… maybe an idea how to solve the problem…

Comment posted by

@OsvaldoCorreia create unique id with javascript