Answering directly
No, addId()
does not exist and addClass()
is not as same as attr()
In HTML, a class
attribute can hold more than one value like <div class="tall fat brown">Someone</div>
so jQuery's function addClass()
helps to add a value to the class attribute not change it. Similarly the functions toggleClass()
and 'removeClass()` help manipulate them.
But the function attr()
will just manipulate the attribute and change it directly.
For example:
<div id="person">someone</div>
And the following jQuery Statements
$("#div").addClass("tall"); // This will create the class attribute and add tall
$("#div").addClass("fat"); // This will append fat to the existing class and make "tall fat"
$("#div").addClass("brown"); // likewise
.addClass()
will just append the class name. If same needs to done by using attr()
you have to do
$("#div").attr('class', 'tall');
$("#div").attr('class', 'tall fat');
$("#div").attr('class', 'tall fat brown');
Or, you can modify the attribute using
$("#div").attr('class', function(i, className) {
return className + " brown";
});
Where as id
s have one value that needs to be modified or altered so a function like addId()
would do exactly which attr('id', 'idvalue') would do.