Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modifying select box option text in jQuery

Given the following HTML:

<select id="addSelection">

<option value="Original Value">Original Value</option>

</select>

I would like to change the text value of the option tag when I click on a button.

Assuming that the button click can be done correctly...how can I change the text of the option tag using jQuery?

So, for example...I would like the option tag changed to this:

<option value="New Value">New Value</option>

I've tried using variations of the .val() function but have not found a solution.

Thank you to all who reply!

like image 966
risingTide Avatar asked Nov 27 '22 22:11

risingTide


1 Answers

The following simplified solution ...

<html>
<head>
    <script type="text/javascript" src="jquery-1.5.2.min.js"></script>
    <script type="text/javascript" >
    function domread() {
        $('#btn').click(function(){
            $('#originalValue').attr('value', 'New Value').html('New Value');
        });
    }
    </script>
</head>
<body onload="domread()">
<select id="addSelection">
  <option id="originalValue" value="Original Value">Original Value</option>
  <option value="Two Value">Value two</option>
  <option value="Three Value">Value three</option>
</select>
<br/>
<input id="btn" type="button" value="Changes"></input>
</body>
</hmtl>
like image 93
Nery Jr Avatar answered Dec 09 '22 19:12

Nery Jr