January 13, 2016
How to Add Style Using jQuery
To add style using jQuery, we make use of css() function. Example is given below.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Tutorial </title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
//Function to apply style.
function myStyle(){
$('#divHello').css('background-color','red');
}
</script>
</head>
<body>
<input type="button" value="Apply CSS" onclick="myStyle()"/> <br /> <br />
<div id="divHello">Hello World! </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>jQuery Tutorial </title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
//Function to apply style.
function myStyle(){
$('#divHello').css('background-color','red');
}
</script>
</head>
<body>
<input type="button" value="Apply CSS" onclick="myStyle()"/> <br /> <br />
<div id="divHello">Hello World! </div>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>jQuery Tutorial </title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> //Function to apply style. function myStyle(){ $('#divHello').css('background-color','red'); } </script> </head> <body> <input type="button" value="Apply CSS" onclick="myStyle()"/> <br /> <br /> <div id="divHello">Hello World! </div> </body> </html>