Reputation: 824
I have a select with 3 values and a hidden input.
<select name="selection">
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<br/>
<input type="hidden" name="hid" value=""/>
I would like to pass the option of the select to the value of the hidden input, and at the same time, if I select 0 from the select, the input type="hidden" become type="text" while maintaining value as 0.
Upvotes: 0
Views: 3647
Reputation: 3909
<select name="selection" class="box1">
<option>0</option>
<option selected>1</option>
<option>2</option>
</select>
<br/>
<input type="text" name="hid" value="" class="box2"/>
$(".box1").change(function () {
var selected = $(this).find("option:selected").text();
$(".box2").val(selected);
if (selected === "0") {
$(".box2").show();
} else {
$(".box2").hide();
}
});
Upvotes: 0
Reputation: 2224
You have to use the change event
$("select[name='selection']").change(function() {
var selected = $(this).find("option:selected").text();
$("input[name='hid']").val(selected);
if (selected == "0")
$("input[name='hid']").attr("type", "text");
else
$("input[name='hid']").attr("type", "hidden");
});
I advise you to read: http://api.jquery.com/change/
Here a jsFiddle: http://jsfiddle.net/V4GZ2/
Upvotes: 1