Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript change select option text

Im trying to modify the text of the first select option via javascript. But it empties the entire select option

 <select name='stuff'>
      <option value="a"> Pepsi </option>
      <option value= "b"> Juice </option>
 </select>


<script type="text/javascript">
    document.getElementsByName('stuff')[0].innerHTML = "Water";
</script>
like image 331
meWantToLearn Avatar asked Sep 19 '14 13:09

meWantToLearn


People also ask

How do I get the text value of a selected option in JavaScript?

function myNewFunction(element) { var text = element. options[element. selectedIndex]. text; // ... }


2 Answers

You want to read from the options collection and modify the first element in there:

document.getElementsByName('stuff')[0].options[0].innerHTML = "Water";
like image 125
CodingIntrigue Avatar answered Sep 17 '22 15:09

CodingIntrigue


You can try this:

 $('select[name=stuff] option:first').html("abcd");
like image 38
Satya Ranjan Sahoo Avatar answered Sep 18 '22 15:09

Satya Ranjan Sahoo