Home > Software engineering >  Checking if a variable is odd VERSUS adding a class in Jquery
Checking if a variable is odd VERSUS adding a class in Jquery

Time:01-05

I want to simply check if a button has been clicked using Jquery, the traditional approach is adding a class to the button once it has been clicked.

However I find this approach more simple:


var active = 0
$('.buttonSelector').on('click',()=>{
 active  = 1
 if (active %2){
     console.log('Not active')
 } else {
     console.log('Button is active')
}
})

Is there something wrong with my approach?

CodePudding user response:

var active = false;
$('.buttonSelector').on('click', () => {
  active = !active;
  console.log(`Clicked: ${active}`)
  if (active) {
    //if active
  } else {
    //if not
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button >Click</button>

  •  Tags:  
  • Related