jquery onclick change class script (.removeClass() and .addClass())
Posted by Raj
JQuery onclick change class (.removeClass() and .addClass())
In this example,I will show How to change class name using Jquery .removeClass() and .addClass() property.
Syntax:
.removeClass(className)
className will be One or more classes to be removed from the class attribute of each matched element.
Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>onclick change class jquery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
.black{
color:#000000;
}
.blue{
color:#0000FF;
}
.red{
color:#FF0000;
}
</style>
<script>
$(document).ready(function(){
$('.Blue').click(function() {
$('#span_id').removeClass().addClass('blue');;
});
$('.Red').click(function() {
$('#span_id').removeClass().addClass('red');;
});
});
</script>
</head>
<body>
<span id="span_id" class="black"><b>Testing jquery onclick change class Example <br />Hello!!!</b></span>
<ul>
<li class="Blue">Blue;
</li>
<li class="Red">Red;
</li>
</body>
</html>
In this example,I will show How to change class name using Jquery .removeClass() and .addClass() property.
Syntax:
.removeClass(className)
className will be One or more classes to be removed from the class attribute of each matched element.
Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>onclick change class jquery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
.black{
color:#000000;
}
.blue{
color:#0000FF;
}
.red{
color:#FF0000;
}
</style>
<script>
$(document).ready(function(){
$('.Blue').click(function() {
$('#span_id').removeClass().addClass('blue');;
});
$('.Red').click(function() {
$('#span_id').removeClass().addClass('red');;
});
});
</script>
</head>
<body>
<span id="span_id" class="black"><b>Testing jquery onclick change class Example <br />Hello!!!</b></span>
<ul>
<li class="Blue">Blue;
</li>
<li class="Red">Red;
</li>
</body>
</html>
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
JAVASCRIPT,
JQuery,
jquery change class,
jquery class onclick,
jquery onclick add class,
jquery set class,
jquery set onclick,
onclick change class,
PHP,
PHP interview questions and answers,
tutorials
.You can leave a response, or trackback from your own site.