Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  184] [ 5]  / answers: 1 / hits: 5532  / 11 Years ago, wed, january 8, 2014, 12:00:00

I have a form that has a dropdown menu, a few text fields and a text area. I would like the form to hide the text area if one of the options from the dropdown menu is selected.



Here is my code:



<form id=contact name=contact action= method=post>
<select name='select-question'>
<option value=member-request>Become a member</option>
<option value=question>Send us your questions / comments</option>
</select>
Name:
<input type=text name=last-name></input>
Comments/questions:</br>
<textarea id=comments name=questions-field rows=5 cols=27></textarea>
<br />
<input type=submit name=submit value=Submit></input>




$(document).ready(function () {
$('#contact select[name=select-question]').change(function () {
if ($('#contact select[name=select-question]').val() == 'question') {
$('#comments').show();
} else {
$('#comments').hide();
}
});
});


I have also posted to JS fiddle: http://jsfiddle.net/7wzUG/5/



I'm very new to JQuery, and I am not sure why this does not work.
Thanks for any help.


More From » jquery

 Answers
3

Include jQuery AND add option:selected to your selector:



$(document).ready(function () {
$('#contact select[name=select-question]').change(function () {
if ($('#contact select[name=select-question] option:selected').val() == 'question') {
$('#comments').show();
} else {
$('#comments').hide();
}
});
});


You also need to hide the comments on load via CSS style and place the label inside the comments div container, so that also the label is invisible when appropriate.



Here's the working fiddle:
http://jsfiddle.net/7wzUG/9/


[#48873] Wednesday, January 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
loric

Total Points: 110
Total Questions: 96
Total Answers: 91

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;