Hello Readers,
When we apply CSS class to HTML element sometimes we don't know that which CSS applied to HTML element.
So, below we use jquery to get the class name.
Here, we use the Jquery .attr() function which returns the class attribute value.
Jquery attr('class')
Syntax:
$("#id").attr('class');
Below is the code to get the class name using jquery functions and use some css to styling the content.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$("#demo").attr('class').split(' ').map(function(clssName){
$("#message").append(clssName +" ");
alert(clssName)
})
});
});
</script>
</head>
<body>
<h1>Get Class Name Using Jquery</h1>
<div class="red green blue" id="demo">red green blue</div>
<p id="message"></p>
<p><input type="button" value="Get Class Name" id="btn"></p>
</body>
</html>
The above code use .attr() function and split function to get the respected class names.
0 Comment(s)