Reputation: 143
Hi I am fairly basic in jQuery, and got stuck in a script:
//hide all radio buttons first
$('#q11_6_1').parent().hide();
$('#q11_6_2').parent().hide();
$('#q11_6_3').parent().hide();
$('#q11_6_4').parent().hide();
$('#q11_6_5').parent().hide();
//check the length of the open text box
var other2text = $('#q11_6_other').val().length;
$('#q11_6_other').keypress(function(){
//if at least one character has been entered, then I want to show all radio buttons
if(other2text>=0)
{
$('#q11_6_1').parent().show();
$('#q11_6_2').parent().show();
$('#q11_6_3').parent().show();
$('#q11_6_4').parent().show();
$('#q11_6_5').parent().show();
}
//else I want to hide all
else
{
$('#q11_6_1').parent().hide();
$('#q11_6_2').parent().hide();
$('#q11_6_3').parent().hide();
$('#q11_6_4').parent().hide();
$('#q11_6_5').parent().hide();
}
});
The first part of the "if condition" is working, however when I have cleared all texts in q11_6_other, those radio buttons won't hide. I think the "else" section is not working, but not sure how to get around it.
Much appreciated with your help!!!
Upvotes: 2
Views: 434
Reputation: 79123
This should do the trick
$('#q11_6_other').keypress(function() {
var other2text = $('#q11_6_other').val().length;
Upvotes: 0
Reputation: 79123
Put the variable other2text inside the event handler
$('#q11_6_other').keypress(function() {
var other2text = $('#q11_6_other').val().length;
Upvotes: 0
Reputation: 79123
Your other2text variable is currently populated once on page load. What you want is make it run everytime you press a button, so put that variable inside the function:
$('#q11_6_other').keypress(function() {
var other2text = $('#q11_6_other').val().length;
...
Upvotes: 0
Reputation: 79123
Put your variable other2text
inside the event hander function:
$('#q11_6_other').keypress(function(){
var other2text = $('#q11_6_other').val().length;
Also, I suggest you use keyup
instead of keypress
. If you do that however, you need to change other2text>=0
to other2text>0
Upvotes: 1