fmineo
fmineo

Reputation: 824

passing value from select to input

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

Answers (2)

Neysor
Neysor

Reputation: 3909

you cant change the type in IE, but you can hide/show it with css.

jsfiddle

HTML

<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"/>

JS

$(".box1").change(function () {
    var selected = $(this).find("option:selected").text();
    $(".box2").val(selected);
    if (selected === "0") {
        $(".box2").show();
    } else {
        $(".box2").hide();
    }
});

Upvotes: 0

Louis XIV
Louis XIV

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

Related Questions