-1

a little help please,

I have this code, I would like to create a javascript function to delete the first three select options (50,55,60) and keep only the last (65) you can help me..... thanks

<select>              
 <option value="50" selected="selected">50</option>
 <option value="55">55</option>
 <option value="60">60</option>
 <option value="65">65</option>
</select>
Giuppe
  • 17
  • 1

2 Answers2

0

give the <select> an id , and check the function below:

function filterSelect(){
var selectobject = document.getElementById("select_id"); 
for (var i=0;i<3;i++){
selectobject.remove(0);      
}       
}

filterSelect();
<select id="select_id">              
         <option value="50" selected="selected">50</option>
         <option value="55">55</option>
         <option value="60">60</option>
         <option value="65">65</option>
        </select>

EDIT: if you want to add condition per option - add it inside the for loop

Eldshe
  • 723
  • 6
  • 19
0

try this

    

    var i = 0;
    while(i < 3){
        document.getElementById('selectId').removeChild(document.getElementById('selectId').getElementsByTagName('option')[0]);
        i++;
    }
    
<select id="selectId">              
 <option value="50" selected="selected">50</option>
 <option value="55">55</option>
 <option value="60">60</option>
 <option value="65">65</option>
</select>
Nbody
  • 1,168
  • 7
  • 33