Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a class (or attribute) if not already there

Tags:

jquery

How would I add an attribute to an element if that element does not already have that attribute?

if (' this does not have class=selected') {

    $(this).addClass('selected');
}
like image 287
David542 Avatar asked Dec 12 '22 02:12

David542


1 Answers

if(!$(this).attr('name')) {
   $(this).attr('name', 'value');
}

that ought to do it.

like image 197
Ben Lesh Avatar answered Jan 05 '23 18:01

Ben Lesh