0

I need to get the ID of an element but the value is dynamic with only the end of it is the same always.

Heres a snippet of the code.

<TABLE ID="_MIPS-LRSYSCPU">

The ID always ends with '-LRSYSCPU' then the _MIPS is dynamic.

How can I get the ID using just JavaScript and not jQuery? thanks

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Dana
  • 11
  • 3

1 Answers1

4

Use the selector [id$="<END OF ID HERE>"]:

const table = document.querySelector('[id$="-LRSYSCPU"]');
console.log(table);
<TABLE ID="_MIPS-LRSYSCPU">

Similarly, by using ^ instead of $, you can select elements who attributes start with a certain string:

const table = document.querySelector('[id^="_MIPS"]');
console.log(table);
<TABLE ID="_MIPS-LRSYSCPU">

And *= to select elements who attributes contain a certain string:

const table = document.querySelector('[id*="LRS"]');
console.log(table);
<TABLE ID="_MIPS-LRSYSCPU">
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320