jQuery > Attribute related method

.attr() - Get set attribute value in jQuery

How to get or set the attribute value of an element in jQuery?


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”. 

 Views: 6535 | Post Order: 27



Write for us






Hosting Recommendations