0

I have a problem here with the following code

$('.paron').change(function() {
   if($(this).is(":checked")) {
          var moduleid = <?php echo $moduleid; ?>;
                var studentid = $(this).attr('id');
                var weeknum = <?php echo $weeknum; ?>;

                undisableTxt(studentid);

             }
       });

So I take the id of a certain tr which has an input and i send it to a function. All good till now. But this is happening when the user check the input [checkbox] when he UNCHECK, how can i have the id to send it to another function???

I've tried the following but no luck

   $('.paron').change(function() {
if($(this).is(":checked")) {
     var moduleid = <?php echo $moduleid; ?>;
     var studentid = $(this).attr('id');
     var weeknum = <?php echo $weeknum; ?>;

             undisableTxt(studentid);

      } else {
        disableTxt(studentid); //error for undefined variable student id.
      }
  });

So i get an error for undefined studentid. FYI each row has another id.

1 Answer 1

1

The variable 'studentId' doesn't exist in your else block. Also you shouldn't define variables with var in blocks, because var doesn't scope variables at the block level, use let or const for that. I also set $this, to save going back and forth into the DOM for performance.

in jQuery you would do it like this.

$('.paron').change(function() {
  const $this = $(this);  
  const studentid = $this.attr('id');

  if($this.is(":checked")) {
    // removed for brevity, but using let or const
    undisableTxt(studentid);
  } else {
    disableTxt(studentid); 
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I also found out another workaround with closest but yours its better! Thanks a lot!! gonna accept it when system let me to!
Thanks, hopefully, my other comments/changes don't seem rude or know-it-all, I just wanted to make the example best as I could.
I'm newbie to jquery and this about const and let helps me a lot cause i've seen let only in swift and i know it has great performance !! much appreciated :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.