Check that the class has a component
-
Hey, help me get this straight. It is not necessary to verify that the class is in the element and, in the absence of a class, add it. Here's the code:
var cells = document.getElementsByTagName('td'); for(var i=0; i<cells.length;i++){ cells[i].addEventListener('click', selectDate); } function selectDate(){ if(cells.classList.contains('active')) { return false; }else{ cells.classList.add('active'); } }
It's full code. http://jsfiddle.net/lilubanana/fwxuf5ea/22/
-
selectDate
- it's a processor's function, inside it. this points to the element on which they clicked. All you need to do is replace cells ♪ thisfunction selectDate(){ if(this.classList.contains('active')) { return false; }else{ this.classList.add('active'); } }
In addition, verification is not necessary in this case, as is classList.add doesn't add the class if he's already present.