1

I am trying to have a select input with an other option when the other option is selected the input should become visible, however I want it defaulted as hidden and it should rehide if select is changed again later. I have tried == and literal === and reversing the hide show commands with != without change

currently I have

HTML:

<tr>
    <td colspan="3" align="center" border="1">Certification:</td>
    <td colspan="3" align="center" border="1">
        <select name="certname" id="certname">
            <option value=""> </option>
            <option value="State">State</option>
            <option value="Other">Other</option>
        </select>
        <input type="text" id="othercert" name="othercert" />
    </td>
</tr>

and JS:

$("#othercert").hide();

$(document).ready(function () {
    $("#certname select").change(function (e) {
        if ($(this).val() != "Other") {
            $("#othercert").hide();
        }
        else {
            $("#othercert").show();
        }     
    });
});

I am unsure where to go from here or what is going wrong - any assistance is appreciated.

1
  • 1
    or can u hard code the css visibilitiy:hidden within the #othercert tag? Commented Sep 17, 2015 at 19:05

2 Answers 2

2

Your selector thinks select is the child to #certname rather than its id. It should be:

select#certname and not #certname select

JS Fiddle

$("#othercert").hide();

$(document).ready(function () {
    $("select#certname").change(function (e) {
        if ($(this).val() != "Other") {
            $("#othercert").hide();
        } else {
            $("#othercert").show();
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

this worked, thanks - oddly enough my syntax worked on another page with a different id name
2

Your change selector is wrong. The select isn't a child of #certname, therefore change to the following:

$("select#certname").change(function (e) {

http://jsfiddle.net/ndgadeb1/


Also you need to put your initial hide inside the document ready function:

  $(document).ready(function () {
      $("#othercert").hide();

However if you wish to hide on page load then you can just do this in CSS:

#othercert{
   display:none;
}

That way it won't flash on page load.


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.