Get class name using jQuery
After getting the element as jQuery object via other means than its class, then
var className = $('#sidebar div:eq(14)').attr('class');
should do the trick. For the ID use .attr('id')
.
If you are inside an event handler or other jQuery method, where the element is the pure DOM node without wrapper, you can use:
this.className // for classes, and
this.id // for IDs
Both are standard DOM methods and well supported in all browsers.
Jquery - how to get class name of div?
Use this code
var className = $('.demo').attr('class');
You can refer here,
http://www.w3schools.com/jquery/html_attr.asp
jQuery get Class Name? Only one class to take
var classFormDiv = $('.class1').attr('class').split(' ')[0];
Try this
How to get class name of element has specific text using javascript/jquery?
You can keep an id for your div, as per your information your text will be unique.
<div id="UniqueText" class="_className">UniqueText</div>
and the js code will be
function getClassNameWhereText(text){
var className = $('#'+text).attr('class');
console.log(className);
}
UPDATE : if you want to using contains
then you can do this,
function getClassNameWhereText(text){
var val = document.getElementById(text).value;
if(text.indexOf(val)>=0){
var className = $('#'+text).attr('class');
console.log(className);
}
}
get the class name by another class name jQuery
If you can accept use dom elements, it has classList which may be what you want.
- Element.classList
elementClasses is a DOMTokenList representing the class attribute of
elementNodeReference. If the class attribute was not set or is empty
elementClasses.length returns 0. element.classList itself is
read-only, although you can modify it using the add() and remove()
methods.
alert($('.myDiv')[0].classList[2]);// Get the count of the classesvar length = $('.myDiv')[0].classList.length;
// Get last class from the list.alert($('.myDiv')[0].classList[length - 1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><div class="myDiv one two three"></div>
get class name with jquery
Use attr function.
e.g.
$.each($target.children(), function(){
alert($(this).attr("class"))
});
How to get alert of class name from selected option in select tag using jquery?
To get the class name of an element, use JavaScript's .className
property.
Syntax:
object.className
Example:
console.log(document.querySelector("#element").className);
For multiple classes, use classList
(only in newer versions):
// Gets fourth class
console.log(document.querySelector("#element").classList[3];
To emulate classList
in older versions, use .split(/\s+/)
// Gets fourth class
console.log(document.querySelector("#element").className.split(/\+s/)[3];
Source: How to get class of clicked element
Edit:
@Andreas was right. The code you have already works and it dosen't need to be modified.
console.log($("#element").attr("class"));
$("#js").text(document.querySelector("#element").className);$("#jq").text($("#element").attr("class"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element" class="my-class"></div>
<code>.className</code> returns: <span id="js"></span><br><code>.attr("class")</code> returns: <span id="jq"></span>
How to get a specify class name from multiple class in a div by using jquery?
Not exactly sure what you are trying to do, but this will get you all the classes for each element:
<div class="class1 class2 class3 class4"></div>
<div class="class1 classb classv classd"></div>
<div class="class1 classw classdf classl"></div>
<div class="class1 classl classo classj"></div>
$('div.class1').each(function() {
console.log( $(this).attr('class').split(' ') )
})
Then you can do an indexOf on the arrays that are returned:
https://jsfiddle.net/nebulousal/y8yoofd4/1/
Related Topics
Show and Hide Divs At a Specific Time Interval Using Jquery
Why Is It Bad Practice to Use Links With the JavaScript: "Protocol"
Confirmation Before Closing of Tab/Browser
How to Avoid the Need For Ctrl-Click in a Multi-Select Box Using JavaScript
Replace HTML Page With Contents Retrieved Via Ajax
How to Download a File Without Using ≪A≫ Element With Download Attribute or a Server
How to Implement Dom Data Binding in JavaScript
Sound Effects in JavaScript/Html5
How to Call a Rest Web Service API from JavaScript
Difference Between the Different Methods of Putting JavaScript Code in an ≪A≫
How to Set Focus on an Element in an HTML Form Using JavaScript
Replace Words in the Body Text
Upload Progress Indicators For Fetch
Get List of Data-* Attributes Using JavaScript/Jquery