Switch to full style
JQuery Examples
Post a reply

Add and Remove CSS classes to html tag dynamically

Mon Dec 31, 2012 11:08 pm

Add and Remove CSS classes to html tag dynamically
Code:

<html>
<
head>
<
title>Change CSS of Element, Add new CSS/ Remove CSS class
/ Toggle </title>

<!--
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
 Note you can download the JQuery package instead of using Google version. --> 
<script src="jquery-1.8.3.js"></script>
<script>
$(document).ready(function(){
  $("button#b1").click(function(){
    $("#p1").addClass("class1 class2");
  });
  
    $("button#b2").click(function(){
    $("#p2").removeClass("class3");
  });
  $("button#b3").click(function(){
  $("#p2").toggleClass("class3");
}); 
});
</script>
<style type="text/css">
.class1
{
font-size:xx-large;
font-weight:bold;
background:#F25201;
height:400px;
width:400px;
}
.class2
{
text-align:center;
text-decoration: underline;
color:blue;
}
.class3
{
color:red;
background:#52F201;
height:400px;
width:400px;
}


</style>
</head>
<body>

<p id="p1">Write your paragraph here, after pressing the button this paragraph will be affected by CSS classes.</p>
<p id="p2" class="class3">CSS affect will be removed from this paragraph</p>
  
<br>
<button  id="b1">Add classes to first paragraph element</button>
<button   id="b2">Remove classes from second paragraph element</button>
<button   id="b3">Toggle class for second paragraph element</button>
</body>
</html>




Post a reply
  Related Posts  to : Add and Remove CSS classes to html tag dynamically
 Remove HTML element or Delete its content     -  
 about classes     -  
 Abstract Classes in jsp     -  
 How to create and use classes in C++     -  
 how do i create the classes XX and YY?     -  
 Remove Indentation     -  
 java wrapper classes     -  
 remove page title     -  
 instantiating java classes     -  
 Add new row dynamically to table using JQuery     -  

Topic Tags

JQuery Content