-2

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.

Osvaldo Correia
  • 426
  • 1
  • 5
  • 13
  • An HTML5 ID can contain a number, but should not *start* with a number (to prevent CSS and JavaScript problems). – Obsidian Age Mar 04 '20 at 01:28
  • @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. – user2740650 Mar 04 '20 at 01:39
  • I got side tracked by the comments, too, and voted to reopen. – Rob Mar 04 '20 at 01:40

2 Answers2

0

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

Richard
  • 1,702
  • 2
  • 11
  • 17
  • 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 – Osvaldo Correia Mar 04 '20 at 01:36
  • 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 – Richard Mar 04 '20 at 01:38
  • @OsvaldoCorreia you can't do that with html alone, you'll need JavaScript. – Richard Mar 04 '20 at 01:40
  • 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... – Osvaldo Correia Mar 04 '20 at 01:40
  • 1
    @OsvaldoCorreia create unique id with javascript https://stackoverflow.com/q/3231459/6514318 – Richard Mar 04 '20 at 01:43
  • Hopefully that helps or at least guides you in the right direction – Richard Mar 04 '20 at 01:44
  • I've read that, I'll take another look at it... Basically I just want to add a simple number or letter to make those duplicate ID, unique as it should be and in doing so, allow me to style the element in CSS. Thank you for your help. – Osvaldo Correia Mar 04 '20 at 01:46
  • @OsvaldoCorreia I'm sorry I couldn't be more helpful as I'm on mobile currently – Richard Mar 04 '20 at 01:50
  • how is that content being generated? Just change the code that generates it to add a unique id to all the tags – Cornel Raiu Mar 04 '20 at 03:12
0

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.

Nuhman
  • 1,172
  • 15
  • 22