Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all select/option lists start by something

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!

like image 950
superscral Avatar asked Jan 15 '23 10:01

superscral


2 Answers

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);
like image 155
Nirvana Tikku Avatar answered Jan 28 '23 06:01

Nirvana Tikku


You can use the getElementsByTagName function to get each SELECT name, for example:

var e = document.getElementsByTagName("select");
for (var i = 0; i < e.length; i++){
  var name = e[i].getAttribute("name");
}

Then you can use the following code to get each OPTION for the SELECT, to do any necessary comparisons:

var options = e[i].getElementsByTagName("option")
like image 38
Justin Ethier Avatar answered Jan 28 '23 05:01

Justin Ethier