Get all select/option lists start by something

13,259

Solution 1

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);

Solution 2

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")
Share:
13,259
superscral
Author by

superscral

One of Duchess France Leader

Updated on June 21, 2022

Comments

  • superscral
    superscral almost 2 years

    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!