$(html).hasClass("class")
, $(document).hasClass("class")
, $(document.html).hasClass("class")
None works. What selector should I use to get the class of the tag ?
Also, how can I find a list of standard elements which I can select in jQuery ?
$(html).hasClass("class")
, $(document).hasClass("class")
, $(document.html).hasClass("class")
None works. What selector should I use to get the class of the tag ?
Also, how can I find a list of standard elements which I can select in jQuery ?
5 Answers
Reset to default 6Try this one:
$('html').hasClass("class");
[!]
You forgot to type quotation aroundhtml
.
Here you can find all jQuery Selectors.
Extra Information
The .hasClass()
method will return boolean
. You can access the class name by getting the class
attribute with .attr()
method:
<div class="a" id="example1"></div>
var c = $('#element').attr('class'); // (string) a
If your element has multiple classes, you can split the string to convert it to an array:
<div class="a b c" id="example2"></div>
var k = $('#example2').attr('class').split(/\s+/); // [a, b, c]
Here is the FIDDLE DEMO.
"What selector should I use to get the class of the tag?"
The other answers use hasClass()
, which returns a boolean not a class:
$('html').attr('class');
<html class="someClass">
</html>
Do this -
$('html').hasClass('someClass');
Your DOM is structured like this, basically:
- document
- documentElement
- head
- body
You can either use jQuery's internal selector engine by writing $('html').hasClass('class')
or you can target the HTML element yourself by writing $(document.documentElement).hasClass('class')
.
If your <div
> has an id:
<div id="test" class="my-custom-class"></div>
...you can try:
var yourClass = $("#test").prop("class");
If your has only a class, you can try:
var yourClass = $(".my-custom-class").prop("class");