Get class attribute in jQuery

Description

The following code shows how to get class attribute.

Example


<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.blue {<!--from   w  w  w  .  ja  v a 2  s  .c  o  m-->
background-color: blue;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>

$(function(){
$("p#myID").attr("class","blue");
var attribute = $("p#myID").attr();
document.writeln(attribute);
});
</script>
</head>
<body>
<p id="myID">
This is a test.
</p>
</body>
</html>

Click to view the demo

The code above generates the following result.

Get class attribute in jQuery
Home »
  Javascript Tutorial »
    jQuery »
      jQuery CSS
...
Add border to all child element in a div el...
Add style from the second with nextAll in j...
Add style to the last paragraph using addCl...
Chain add method with addClass method in jQ...
Chain .width() with .css() function in jQue...
Change CSS properties value in mouse hover ...
Change background color with rgb function i...
Change element id and text in its mouse hov...
Change style for elements with specific id ...
Change the border for selected element in j...
Change the font style for last table row in...
Change width by setting the css width prope...
Check if an element has a certain class nam...
Check if an element has the style I need in...
Click to get the height in jQuery
Display the left and top properties returne...
Get class attribute in jQuery
Get height for a selected element in jQuery
Get height for the whole document in jQuery
Get inner height for a div in jQuery
Get outer width including margin in jQuery
Get the background color and create a new s...
Get the element background color in jQuery
Get the height for a tag in jQuery
Get the inner width for a div element in jQ...
Get the offset of last b tag in jQuery
Get the out height of a div element in jQue...
Get the outer width of an element in jQuery
Get the scroll bar horizontal position in j...
Get the scroll top offset in jQuery
Get the top and left position in jQuery
Get the whole document height in jQuery
Get the window height in jQuery
Print offset for an event in jQuery
Remove class from the first paragraph in jQ...
Remove the class attribute from selected di...
Reposition an element with .offset() and co...
Set background color for a form element in ...
...