jQuery add or remove class dynamically on button click

jQuery has addClass() and removeClass() methods which makes it really easy to dynamically add or remove CSS class from HTML page elements. In this example we are going add and remove a class from the paragraph based on a button click. Here is how to use ...

jquery add remove class onclick
jquery add remove class onclick

<title>jQuery add and remove CSS class example</title>

 rel="stylesheet" type="text/css" />
<style type="text/css">

.shaded {
 background-color: #eeeee0;

<script type="text/javascript">
        $(document).ready(function() {
         $("#addClass").click(function () {
            $("#removeClass").click(function () {

   <legend>jQuery dynamically ADD and REMOVE CSS class</legend>

   <p id="paragraph1">
    <label for="myInputText1">
     My first input text is here:
    </label><br /> 
     id="myInputText1" type="text" name="inputBox" />
   <p id="paragraph2">
    <label for="myInputText2">
     My second input text is here:
    </label><br /> 
     id="myInputText1" type="text" name="inputBox" />
   <input id="addClass" type="button"
     value="Add background color" />
   <input id="removeClass" type="button"
     value="Remove background color" />  

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.