I have a page which changes the ID of input fields every time. So for example if I visit the page now, the ID can be "stack_15_overflow"
and next time it can be "stack_293_overflow"
.
I want to use a wildcard value for getElementById
, such as "stack_ * _overflow"
(where *
matches anything), to get that value related to any input field starting and ending with some specific text, no matter what text is in between.
Code:
function HelloId(string){
var name=string
document.getElementById('stack_*_overflow').value=name;
}
Using jQuery's attribute starts with
and attribute ends with
selectors:
$("[id^='stack'][id$=overflow]");
Note that these selectors are expensive, specifying type of the element can improve the performance:
$('element').filter("[id^='stack'][id$=overflow]");
var elements = document.querySelectorAll('[id^="stack_"][id$="_overflow"]');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With