javascript on click function

Permalink
I've some code on a form that successfully adds shows two hidden attributes and also adds a required class on both upon a check box being clicked. When it is unchecked nothing happens. What's missing?

$('.recruite_to_team input').click(function(){
        if(
          $('.recruite_to_team input').is(':checked')
        ){
            $('.attendee_email').show();
            $('.team_code').show();
          $('.team_code .input').addClass('required');
          var reqd = $('.team_code .input').find('.required_indicator').html();
          if(!reqd){
              $('.team_code .input').prepend(' <i class="required_indicator">*</i>');
          $('.attendee_email .input').addClass('required');
          var reqd = $('.attendee_email .input').find('.required_indicator').html();
          if(!reqd){
              $('.attendee_email .input').prepend(' <i class="required_indicator">*</i>');    
            }

View Replies:
ramonleenders replied on at Permalink Reply
ramonleenders
Try something like this:

$(document).on('change', '.recruite_to_team input', function(){
   var checked = $('.recruite_to_team input').is(':checked');
   if(checked){
      $('.attendee_email').show();
      $('.team_code').show();
      $('.team_code .input').addClass('required');
      var reqd = $('.team_code .input').find('.required_indicator').html();
      if(!reqd){
         $('.team_code .input').prepend(' <i class="required_indicator">*</i>');
         $('.attendee_email .input').addClass('required');
         var reqd = $('.attendee_email .input').find('.required_indicator').html();
         if(!reqd){
            $('.attendee_email .input').prepend(' <i class="required_indicator">*</i>');
         }
      }else{
justynpride replied on at Permalink Reply
Thanks. Unfortunately I get the same end result as with the original code. That is when I uncheck the box the fields stay appearing, but when I check the box again the fields disappear.

> On 12 Oct 2017, at 17:30, concrete5 Community <[email protected]> wrote:
ramonleenders replied on at Permalink Reply
ramonleenders
Put an "alert('hello there');" into the "else" and see if it gets there. If it does, you need some code changes within the else. Not sure what you're trying to do here with all these classes and such so, probably best to do a thing like that.
justynpride replied on at Permalink Reply
The alert does appear, but only when I've unchecked, and then checked the box again.
weyboat replied on at Permalink Reply
weyboat
Text Deleted as it referenced a deprecated jquery function..
justynpride replied on at Permalink Reply
Sorted. There was an extra } in the code. I've removed and it's all working.