myWallJSON myWallJSON - 1 year ago 50
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 Source

try this

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