jQuery – get class list of DOM element

jQuery code snippet to get class list of an element. We are using jQuery attr("class") to get the class list as string and then splitting it on space.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<div id="id1" class="class1 class2 class3">text inside div</div>
<pre id="msg"></pre>

<script type="text/javascript">
var classes = $("#id1").attr("class");
var classes_arr  = classes.split(" ");
$("#msg").text("div classes: " + JSON.stringify(classes_arr));
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments