Get & set drop down value with jQuery

If the user selects an option from the drop-down list, he will display this text box, but if he selects the option with the value "Other", a line will appear that will enter a value for another.

my code works fine if only the parameter value is not "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 or set a value in the text box.

If he allows jquery, then I will be grateful for the code.

Thank.

+3
source share
2 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 call. Try it here .

+5
source

karim79 , :

$(function() {
    $("select[name=choice1]").change(function() {
       $("#ShowHide").toggle($(this).val() === "other");
    });
});
  • $(document).ready(f), do $(f),
  • ==, === ( , == , )
  • toggle(b) show()/hide()
+1

Source: https://habr.com/ru/post/1744791/


All Articles