jim
jim

Reputation: 3

select box manipulation

I would like to dynamically change the option value of a select tag. Is this possible? When I say value, I mean the "Change Me" portion. If this is possible, can someone please show me how?

<option value="0">Change Me</option>

Upvotes: 0

Views: 848

Answers (2)

S P
S P

Reputation: 4643

You can fetch and modify the combobox item's text as follows:

$("#MyCombo option:selected").text()

Note: @shaz; thanks for your comment, my mistake!

Upvotes: 1

Ibu
Ibu

Reputation: 43810

Yes it is possible

You will need to look at the options available using a select tag.

<select id='selection'>
 <option value='0'>Value 0</option>
 <option value='1'>Value 1</option>
 <option value='2'>Value 2</option>
 <option value='3'>Value 3</option>
 <option value='4'>Value 4</option>
 <option value='5'>Value 5</option>
</select>

the javascript

var select = document.getElementById('selection');

// to get the currently selected item, use the `selectedIndex` property.
var index = select.selectedIndex; // 3 in case of value 2 selected

// to change its text
select.options[index].innerHTML = 'the new value';

is this what u are looking for?

Upvotes: 0

Related Questions