Home > Software engineering >  JQuery selector, random numbers in selector
JQuery selector, random numbers in selector

Time:09-02

How can I handle random numbers in selector?

This syntax is from Power Automate but it uses Jquery selectors.

This is the selector that I have : input[Id="SalesLine_SalesPrice_2437_0_0_input"] but I figured that _2437_ Might be any number, for example 1, 25, 102, 2467, 35697

So how I can handle that random number?

CodePudding user response:

To do what you need you can use a combination of the 'attribute begins with' and 'attribute ends with' selectors:

$('input[id^="SalesLine_SalesPrice_"][id$="_0_0_input"]')

Or the 'attribute contains' selector:

$('input[id*="SalesLine_SalesPrice"]')

Or filter() with a regex:

$('input').filter((i, el) => /SalesLine_SalesPrice_\d{1,5}_0_0_input/i.test(el.id));

It would depend on your use case which is the most appropriate.

That being said, if you want to select multiple of these elements with dynamic id attributes, then the best approach would be to just add a common class to them all.

$('input.common-class')
  • Related