2

I'm trying to create a "How did you find us form element" and I'm having some jQuery trouble. The user selects from a list of options, one of which is "other". When selected other a text box that allows them to be more specific. In an effort to make this more user friendly that input is hidden when another option is displayed. I've got the jQuery working to show and hide the text input as the user changes the option but I would like it to clear any text in the text box in the event the user selects other, fills something in, then selects another option.

<label for="pcFindUs">How did you hear about us?</label>
        <select name="pcFindUs" id="pcFindUs" onChange="getval();">
          <option value="No Answer">Select One</option>
          <option value="Internet Search">Internet search</option>
          <option value="Internet Advertisement">Internet ad</option>
          <option value="Soclail Media">Social media </option>
          <option value="Unknown">I don't remember</option>
          <option value="other">Other</option>
        </select><br/>
        <div id="pcHiddenOtherSpecify" style="display:none;">
          <label for="pcFindUsSpecify">(Please Specify): </label><input type="text" value="" id="pcFindUsSpecify" name="pcFindUsSpecify" maxlength="50">
        </div>
        <script>
          function getval(){
            var values = $('#pcFindUs :selected').val();
            if (values == "other"){
              $("#pcHiddenOtherSpecify").css("display","block");
            }else{
              $("#pcHiddenOtherSpecify").attr("value","");
              $("#pcHiddenOtherSpecify").css("display","none");
            }
          }
        </script>

The pcHiddenOtherSpecify div containing the additional input appears and disappears just fine, but the value of #pcHiddenOtherSpecify still has whatever the user entered. I've also tried

$("#pcHiddenOtherSpecify").val("");

With no luck. Any ideas what I may be doing wrong here?

3 Answers 3

8

You are trying to change the value of a div element, not an input. Try this:

$("#pcFindUsSpecify").val("");
Sign up to request clarification or add additional context in comments.

1 Comment

That was my mistake. Thanks.
6

Wrong ID

$("#pcFindUsSpecify").val("");

Comments

1

try $("#pcFindUsSpecify").val("");

check it out http://codepen.io/JcBurleson/pen/MKBBWq

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.