开发者

select box manipulation

开发者 https://www.devze.com 2023-03-09 04:34 出处:网络
I would like to dynamically change the option value of a select tag. Is this possible? When I say value, I me开发者_StackOverflow社区an the \"Change Me\" portion. If this is possible, can someone plea

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

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


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

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

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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号