.toggleClass()
Add or remove a class from each element in the set of matched elements.
Syntax
.toggleClass(className)
- className: A class name to be toggled in the class attribute of each element in the matched set
.toggleClass(className, addOrRemove)
- addOrRemove: A Boolean indicating whether to add or remove the class
.toggleClass(function[, addOrRemove])
- function: A function that returns a class name to be toggled in the class attribute of each element in the matched set. addOrRemove (optional): A Boolean indicating whether to add or remove the class
Return value
The jQuery object, for chaining purposes.
Description
For example, we can apply .toggleClass() to a simple <div> as follows:
<div class="tumble">Some text.</div>
The first time we apply $('div.tumble').toggleClass('bounce'), we get:
<div class="tumble bounce">Some text.</div>
The second time we apply $('div.tumble').toggleClass('bounce'), we get:
<div class="tumble">Some text.</div>
$('#foo').toggleClass(className, addOrRemove);
is equivalent to
if (addOrRemove) {
$('#foo').addClass(className);
}
else {
$('#foo').removeClass(className);
}
The .toggleClass() method allows us to indicate the class name to be toggled by passing in a function.
$('div.foo').toggleClass(function() {
if ($(this).parent().is('.bar') {
return 'happy';
} else {
return 'sad';
}
});
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function () {
$(this).toggleClass("blue");
});
});
</script>
</head>
<style>
.blue { color:blue; }
</style>
<body>
<body>
<p class=blue>A</p>
</body>
</html>
Toggle DIV color
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#run").click(function(){
$("div").toggleClass("colored");
});
});
</script>
<style>
div { background:yellow; width:80px; height:80px; margin:5px; float:left; }
div.colored { background:green; }
</style>
</head>
<body>
<button id="run">Run</button>
<div>asdf</div>
<div id="mover">asdf</div>
<div>asdf</div>
</body>
</html>
Home
JavaScript Book
jQuery
JavaScript Book
jQuery
DOM:
- jQuery DOM
- $("html tags"):generate code with the jQuery wrapper function.
- .add()
- .addClass()
- .after()
- .andSelf()
- .append()
- .appendTo()
- .attr()
- .before()
- .children()
- .clone()
- .closest()
- .contents()
- .css()
- .detach()
- .filter()
- .first()
- .get()
- .has()
- .hasClass()
- .height()
- .html()
- .index()
- .innerHeight()
- .innerWidth()
- .insertAfter()
- .insertBefore()
- .is()
- .last()
- .map()
- .next()
- .nextAll()
- .nextUntil()
- .not()
- .offset()
- .offsetParent()
- .outerHeight()
- .outerWidth()
- .parent()
- .parents()
- .parentsUntil()
- .position()
- .prepend()
- .prependTo()
- .prev()
- .prevAll()
- .prevUntil()
- .remove()
- .removeClass()
- .removeAttr()
- .replaceAll()
- .replaceWith()
- .siblings()
- .scrollLeft()
- .scrollTop()
- .slice()
- .text()
- .toArray()
- .toggleClass()
- .unwrap()
- .val()
- .wrap()
- .wrapAll()
- .wrapInner()
- .width()