To get or set a particular attribute of an element, attr()
method can be used.
<script>
$("#div2").attr('class', 'class2');
alert($("#div2").attr('class'));
</script>
In above code snippet, the first line of code will set the class
attribute as “class2” of the div element whose id as “div2”. The second line of code will show alert with the value of class attribute for the element whose id is “div2”.