In this example, we will learn how to add or remove styles in Jquery.
Syntax to Apply Style
 $("#elementId").addClass("cssClassName");
Syntax to Remove Style
 $("#elementId").removeClass("cssClassName");
Example Program
addRemoveCss.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>JQuery CSS Example</title> 
<link type="text/css" href="styles/addRemoveCss.css" rel="stylesheet" /> 
</head> 
<script type="text/javascript" src="lib/jquery-1.8.0.min.js"></script> 
<script type="text/javascript" src="js/addRemoveCss.js"></script> 
<body> 
  <input type="button" id="changeColorBtn" value="Change Color" /> 
  <br> 
  <br> 
  <div id="myDiv" style="width: 375px">Div</div> 
</body> 
</html> 
 | 
addRemoveCss.css
@CHARSET "ISO-8859-1"; 
.redColor { 
  background: red; 
} 
.blueColor { 
  background: blue; 
} 
 | 
addRemoveCss.js
$(document).ready(function() { 
  $("#changeColorBtn").click(function() { 
    $("#myDiv").addClass("redColor"); 
    alert("redColor added"); 
    $("#myDiv").addClass("blueColor"); 
    alert("blueColor added....."); 
    $("#myDiv").removeClass("blueColor"); 
    alert("blueColor removed"); 
  }); 
}); 
 | 
No comments :
Post a Comment