connectseekers.com

Friday, 8 January 2016

jQuery add remove css class onclick

1/08/2016 - By Pranav Singh 0

In this article I will show you how you can add remove css class style sheet in a div control on button click using jQuery. This article example you can use in you Asp.net as well as MVC application.

 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>


Now we will add the below code also in header of the page.


<style type="text/css">
        .firstcssclass {
            font-size: 15px;
            width: 200px;
        }

        .secondcssclass {
            font-weight: bold;
            font-size: 20px;
            width: 300px;
        }
    </style>
    <script language="javascript">
        function AddRemoveFirstClass() {
            $("#divText").removeClass("secondcssclass").addClass("firstcssclass");
        }
        function AddRemoveSecondClass() {
            $("#divText").removeClass("firstcssclass").addClass("secondcssclass");
        }
    </script>


In above code I have created a style sheet I will show you how you can switch between the css class. In javascript code I have used jQuery remove and add class function.

Now please check he completer code.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jquery add remove css class onclick</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
    <style type="text/css">
        .firstcssclass {
            font-size: 15px;
            width: 200px;
        }

        .secondcssclass {
            font-weight: bold;
            font-size: 20px;
            width: 300px;
        }
    </style>
    <script language="javascript">
        function AddRemoveFirstClass() {
            $("#divText").removeClass("secondcssclass").addClass("firstcssclass");
        }
        function AddRemoveSecondClass() {
            $("#divText").removeClass("firstcssclass").addClass("secondcssclass");
        }
    </script>
</head>
<body>
    <div id="divText">
        This is s demo css class for you.
    </div>
    <br />
    <input type="button" onclick="javascript: AddRemoveFirstClass();" value="Button 1" />
    <input type="button" onclick="javascript: AddRemoveSecondClass();" value="Button 2" />
</body>
</html>



In above code I have taken a div control and two button control in this I have called the javascript function on button click on the button control.

Now we have down run the code and check the output. When we run the code our page will look as shown below.


 Now click on button 1 to get below output.




Now click on button 2 to get the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top