I’m updating the selected option programmatically using jQuery, but nothing changes in the browser. (That is, the old selected option remains selected instead of switching to the newly selected option.) Suggestions?
Thanks. –Jeff
I have a simple form like this:
<form id="form1" name="form1" method="" action="">
<p>Assign:
<select name="assigner" id="assigner">
<option value="Sam" selected="selected">Sam</option>
<option value="Harry">Harry</option>
<option value="Fred">Fred</option>
</select>
<input type="button" name="button1" id="button1" value="Submit" />
</p>
<p> Task A: <select name="assignment[]" id="assigner">
<option value="Sam">Sam</option>
<option value="Harry" selected="selected">Harry</option>
<option value="Fred">Fred</option>
</select>
</p>
<p>
Task B: <select name="assignment[]" id="assigner">
<option value="Sam">Sam</option>
<option value="Harry" selected="selected">Harry</option>
<option value="Fred">Fred</option>
</select>
</p>
</form></div>
and my jQuery code looks like this:
<script type="text/javascript">
jQuery(document).ready(function(){
$('[name="button1"]').click(
function(){
var form = $(this).parents('form');
var assigned = form.find(':selected').first().val();
form.find(':selected').each(function(index){
$(this).val( assigned ).change();
});
}
);
});
</script>
Not as far as I can see. You’re re-setting the value of the selected
option, but not doing anything as far as I can tell to the actualselectbox.To change a
selectbox, you need to identify it, then callvalon it, not one of itsoptionelements.I can’t make out what you want your
input[name="button1"]to do, but here’s an example of updating aselectbox: Live copy | sourceHTML:
JavaScript:
Separately, as j08691 pointed out in the comments, you can’t assign the same
idvalue ("assigner") to more than one element.idvalues must be unique in the document. Your code doesn’t show you using thatid, so this may well be unrelated, but it’s worth flagging up.