Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept any characters in between * of an getElementById (stack_ * _overflow)? [duplicate]

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;
}
like image 955
Byzantine Avatar asked Aug 26 '13 17:08

Byzantine


2 Answers

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]");
like image 155
undefined Avatar answered Nov 01 '22 07:11

undefined


var elements = document.querySelectorAll('[id^="stack_"][id$="_overflow"]');
like image 20
Paul Avatar answered Nov 01 '22 08:11

Paul