6

I want to get class/attr of each checkbox. Code sample is given below.

  jQuery("input[type='checkbox']").each(()=> {
      let checkboxID = jQuery(this).attr("class");
      console.log(checkboxID);//output undefined
      console.log(this.atc1List); //typescript variable 
    });

1 Answer 1

17

Inside arrow function this refers to your class instance so update your code as follows, where the second argument in callback refers to the element.

jQuery("input[type='checkbox']").each((i, ele) => {
  let checkboxID = jQuery(ele).attr("class");
  console.log(checkboxID);//output undefined
  console.log(this.atc1List); //typescript variable 
});

As per MDN Docs:

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.

Sign up to request clarification or add additional context in comments.

1 Comment

@PalashKantiBachar : glad to help you

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.