1

I am trying to learn jQuery, by putting what I've learned into practice. So, here's my coding problem.

I have two buttons, one "yes" and one "no". Once the "Yes" radio button is selected, a new text input is to appear.

my jquery below:

$(document).ready(function(){ 
    $("#many").hide();
var isChecked = $("#dform input" ).change(function() {
    $("input[name=question]:checked", "#dform").val();  
  if(isChecked == yes){
    $("#many").show();
  }else {$("#many").hide();}
   });
});

my code-pen

I used this answer added the value function in a variable, then used it in a conditional statement. I put the value $("input[name=question]:checked", "#dform").val(); in an alert to make sure that the value attached to the "yes" radio button has been captured. However, when I put the .val() function in a variable and use it in a conditional statement, it doesn't work.

Did I set the Jquery up correctly?

Community
  • 1
  • 1
hnewbie
  • 151
  • 1
  • 15

5 Answers5

1

Try something like this

<script>
$(document).ready(function () {

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

    $("input[name='question']").click(function () {

        if ($('#yes').is(':checked')) { $("#many").show(); } 

        else { $("#many").hide(); }
    });
});
</script>
Shrikant Mavlankar
  • 1,145
  • 1
  • 8
  • 17
1

You're a little bit off track. You don't need to name your onChange handler (var isChecked...) and I think that's confusing you. I would try something more along the lines of this:

$(document).ready(function(){ 
  $("#many").hide();
  $("#dform input[name=question]").change(function() {
    if ($('#dform input[name=question]:checked').attr('value') == 'yes') {
      $("#many").show();
    } else {
      $("#many").hide();
    }
  });
});
LGFaler
  • 126
  • 1
  • 10
  • so by putting the onChange handler in a variable, made the "yes" value undefined? – hnewbie Jul 21 '16 at 18:39
  • @hnewbie I'm not sure I understand your question. Can you rephrase? – LGFaler Jul 21 '16 at 18:43
  • By putting `$("input[name=question]:checked", "#dform").val();` in the variable that I set, isChecked, did it make the value captured through the .val() function undefined? – hnewbie Jul 21 '16 at 19:22
  • @hnewbie if you did `var isChecked = $('#dform input[name=question]:checked').val();` that would have been just fine. Your original code, had `var isChecked = $("#dform input" ).change(function() { ...`, though. Later you tried referencing `isChecked`, but that variable was only assigned to your handler, not to the actual result you were looking for. – LGFaler Jul 21 '16 at 20:35
1

Try this one to hide and show an element

$(document).ready(function(){ 
$("#many").hide();
 $("#dform input" ).change(function() {
     $("#many").show();
  });
  $("#no").change(function(){
    $("#many").hide();
  })
});

change html as:

<input type="radio" name="question" id="no" value="no" checked>
Syed Aqeel
  • 1,009
  • 2
  • 13
  • 36
1

There are different way to do it simple way to do it with Jquery addClass and Remove Class method

**HTML**
<h4>
Click on button to toggle
</h4>
<div style="padding:10px;">
<button class="show btn">Show</button>
<button class="hide btn">Hide</button>

</div>
<div>
<input type="text" id="name" class="displayN"/>
</div>

Jquery

$(document).ready(function(){

$(".show").click(function(){
    $("#name").addClass('displayB').removeClass("displayN");
});

$(".hide").click(function(){
    $("#name").addClass("displayN").removeClass("displayB");
})

});

Working Example: https://jsfiddle.net/mshyam83/u3wfpznn/

Shyam Mohan
  • 119
  • 2
  • 7
0

Please replace your js with this:

$(document).ready(function(){ 
    $("#many").hide();
    $("#dform input" ).change(function() {
    var val = $("input[name=question]:checked", "#dform").val();  
     if (val == "yes") {
       $("#many").show();
     } else {
       $("#many").hide();
     }
   });
});

I'm sure it could be cleaned up more, but its working anyway. The jQuery set up is fine. Code Pen: http://codepen.io/anon/pen/GqxGmp