0

I am trying to create a scenario where I am showing different content on switching the radio button. it is working fine in chrome but not working in IE. Cant figure out what is going wrong.

<div id="Q6">
  <input type="radio" id="Q6A" value="yes" name="Q6"/>Same as current address<br/>
  <input type="radio" id="Q6B" value="no" name="Q6"/>New address<br/>
</div>

<div id="Q6Aoutput" style="display:none;">1</div>
<div id="Q6Boutput" style="display:none;">2</div>

<script>
$('#Q6').change(function(){
  if($('#Q6A').attr('checked')){
    $('#Q6Aoutput').show();
  }else{
    $('#Q6Aoutput').hide();
  }
  //
  if($('#Q6B').attr('checked')){
      $('#Q6Boutput').show();
  }else{
      $('#Q6Boutput').hide();
  }
});
</script>
Cœur
  • 37,241
  • 25
  • 195
  • 267
soum
  • 1,131
  • 3
  • 20
  • 47

4 Answers4

1

Try replacing:

if($('#Q6A').attr('checked')){

With:

if($('#Q6A').prop('checked')){

And:

$('#Q6').change(function(){

You added a change event to the div, while it's valid as events do bubble, I would have use this instead:

$('input[name="Q6"]').click(function(){
    $('#Q6Boutput').toggle(this.id == "Q6B");
    $('#Q6Aoutput').toggle(this.id == "Q6A");
});
gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Did you try the is(:selector) method of jQuery? Also, it may be better to attach the event to the input radios directly.

$('#Q6A').change(function(){
  if($(this).is(':checked')){
    $('#Q6Aoutput').show();
  }else{
    $('#Q6Aoutput').hide();
  }
});
$('#Q6B').change(function(){
  if($(this).is(':checked')){
      $('#Q6Boutput').show();
  }else{
      $('#Q6Boutput').hide();
  }
});
SuperSkunk
  • 1,268
  • 12
  • 24
0

$('#Q6') selects the <div>, not the <input>.

You could use $('#Q6 input')

idrumgood
  • 4,904
  • 19
  • 29
0

There is a native javascript way to check it, see this question and answer.

So in your case:

if($('#Q6A').checked){
    //etc
}
Community
  • 1
  • 1
MarioDS
  • 12,895
  • 15
  • 65
  • 121