After creating a new html element
$('select#base_element').append('<option value="new_option" id="option_'+id+'">New option</option>');
How do I do:
$('#option_'+id).text('new text');
id is a global variable that get updated each time a new option is added
Below is full example of what I’m trying to do:
<html>
<head>
<script src="jquery.min.js"></script>
<script type="text/javascript">
var id = 0;
function addOption() {
$('#myselect').append('<option id="option_'+id+'" value="blah">blah '+id+'</option>');
$('#buttons').append('<input type="button" onclick="changeText('+id+');" value="Change text of option '+id+'" />');
id += 1;
}
function changeText(target_id) {
alert('Change Text clicked');
$('#option_'+id).text('New text for option '+target_id);
}
$(function() {
addOption();
addOption();
addOption();
});
</script>
<head>
<body>
<select id="myselect">
</select>
<div id="buttons"></div>
</body>
</html>
Try like this