0

I have tried to use setAttribute() method in jquery as mentioned below :

$(p).setAttribute('class','first');

However that didn't work so I replaced setAttribute with attr() method.

$(p).attr('class','first');

It worked when I used the above code.

I just want to know why it didn't work when I used setAttribute() and why it worked when I used attr()?

Harish
  • 1,193
  • 6
  • 22
  • 45

1 Answers1

5

.setAttribute() is a function in javascript works on DOM element where .attr() is a jQuery function works on jQuery object (DOM Object)

you can check it by doing

$(p)[0].setAttribute('class','first');

OR

p.setAttribute('class','first'); // no need to wrap in $
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68