myWallJSON myWallJSON - 6 months ago 8
Javascript Question

How to add class to element only if it already does not have it?

How to add class to element only if it already does not have it? Say we dont know if element has

class="desired_class ...
but we want to make shure it has.

Answer

try this

var elem = $('selector');
if(!elem.hasClass('desired_class')){
  elem.addClass('desired_class');
 }
Comments