Get all select/option lists start by something

superscral picture superscral · Jan 23, 2013 · Viewed 12.6k times · Source

In an HTML page i have severals list.

<select name="salut-1358937506000-OK">
<option selected="" value="OK">OK</option>
<option value="OK">NOK</option>
</select>

<select name="salut-1358937582000-OK">
<option selected="" value="OK">OK</option>
<option value="OK">NOK</option>
</select>
...

In javascript, I want to get all select/option list which started by "salut-". For theses list, i want to compare his name and his selected value.

I know it is possible in jQuery but can't use jquery, only javascript (JSNI with GWT exactly).

Have you an idea?

Thanks!

Answer

Nirvana Tikku picture Nirvana Tikku · Jan 23, 2013
var selects = document.getElementsByTagName('select');
var sel;
var relevantSelects = [];
for(var z=0; z<selects.length; z++){
     sel = selects[z];
     if(sel.name.indexOf('salut-') === 0){
         relevantSelects.push(sel);
     }
}
console.log(relevantSelects);