javascript - Making Buttons Do Different Things -


so have code...

it works, not how want to.

$('.yesorno').on('click', function() {    var $yes = $("#yes").on('click');    var $no = $("#no").on('click');    if ($yes) {      $("#atsize").show();      $("#ada").hide();    } else if ($no) {      $("#ansize").show();      $("#ada").hide();    }  });

what want do, make if clicks yes, directed html form a, , if click no, directed html form b

you need check id in condition.

$('.yesorno').on('click' , function(){     var $yes = $(this).attr('id') === 'yes'; var $no = $(this).attr('id') === 'no';  if($yes){ $("#atsize").show();     $("#ada").hide(); } if($no){ $("#ansize").show();     $("#ada").hide(); } }); 

if there's 2 choices - can simplified to:

    $('.yesorno').on('click' , function(){         var $isyes = $(this).attr('id') === 'yes';      if($isyes){     $("#atsize").show();         $("#ada").hide();     } else {  $("#ansize").show();         $("#ada").hide(); }     }); 

another way of doing it:

    $('.yesorno').on('click' , function(){ var bool = $(this).attr('id') === 'yes'; var eltohide = bool ? '#atsize' : '#ada'; var eltoshow = bool ? '#ada' : '#atsize'; $(eltohide).hide(); $(eltoshow).show();     }); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -