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
  • 351,820
  • 24
  • 303
  • 358
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
  • 313,535
  • 40
  • 245
  • 254