Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get & set drop down value using jQuery

If user select an option from dropdown it will shown it a textbox but if he select option having value "Other" then a row will appear to type value for other.

my code works fine except when option value is not equal to "Other"

<script type="text/javascript"><!--
function setAndReset(box) {
if(box.value == 'Other'){
    $("#ShowHide").hide();
}
document.FormName.hiddenInput.value = box.value;

}
//-->
</script>

<body bgcolor="#ffffff">
<form id="FormName" action="" method="get" name="FormName">
    <select name="choice1" size="1" onchange="setAndReset(this);">
    <option value="one">first</option>
    <option value="two">second</option>
    <option value="three">third</option>
    <option value="other">Other</option>
    </select>

    <input type="text" name="hiddenInput" value="">
    <tablt><tr id="ShowHide"><td>
        <input type="text" name="otherInput">
    </td></tr></table>
    <input type="submit" name="submitButtonName">
</form>
</body>

but it does not show/hide & does not set value in textbox.

If it's solve using jquery then i will be thankful to you for you code.

Thanks.

like image 274
PHP Ferrari Avatar asked May 11 '26 14:05

PHP Ferrari


1 Answers

Do it like this:

$(document).ready(function() {
    $("select[name=choice1]").change(function() {
       $(this).val() == 'other' ? $("#ShowHide").show() : $("#ShowHide").hide();
    });
});

and lose the inline onchange function call. Try it here.

like image 77
karim79 Avatar answered May 13 '26 04:05

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!