Click here to Skip to main content
15,890,882 members
Articles / Web Development / CSS

Overwrite CSS Styles Using addClass In jQuery

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
22 Oct 2015CPOL2 min read 11.8K   3
In this post we will see how we can overwrite CSS styles using addClass method in jQuery.

In this post we will see how we can overwrite CSS styles using addClass method in jQuery. We have so many options to change the styles of a particular element. We can do it by editing the style sheet manually.

In this post we are applying more CSS rules to the same element. I hope you will like this.

Here I am using jQuery 2.0.2 version, you can use whichever version you like.

Background

Recently I have got a requirement of changing a CSS property of an element which is common for element. So I wanted to change the CSS property only if my condition becomes true. In this situation I used jQuery addClass to do this requirement. Here I will show you how.

Using the code

First thing you need to do is create a page.

<!DOCTYPE html>
<html>
<head>
    <title>AddClass JQuery Demo - Sibeesh Passion</title>
</head>
<body>
   AddClass JQuery Demo - Sibeesh Passion
</body>
</html>

Then you need to include the script needed.

<script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>

Now you need to write the scripts.

<script>
        $(document).ready(function () {
            setTimeout(function () {
				$('.first').addClass('second');
			}, 5000);
        });
    </script>

We are adding a new CSS style second to a class first . We will add this after five seconds of document ready. So now we need to create the styles right?

<style>
      .first {
          width: 100px;
      height: 100px;
          border: 1px solid #ccc;
          border-radius: 5px;
          padding: 10px;
          margin: 10px;
          box-shadow: 1px 1px 1px #999;
          font-style: oblique;
          text-align: center;
          background:green;
      }
  .second {
          width: 200px;
      height: 200px;
          border: 1px solid #ccc;
          border-radius: 5px;
          padding: 10px;
          margin: 10px;
          box-shadow: 1px 1px 1px #999;
          background: blue;
          font-style: oblique;
          text-align: center;
      }
  </style>

Now please run your browser, you can see the following output.

Overwrite CSS Styles Using addClass

Overwrite CSS Styles Using addClass

Now after five seconds, the same element will look as follows.

Overwrite CSS Styles Using addClass

Overwrite CSS Styles Using addClass

If you look at your browser console, you can see both of the styles are applied to the element.

Overwrite CSS Styles Using addClass

Overwrite CSS Styles Using addClass

That’s all, now we can see the complete code here.

Here I am using setTimeout as my condition, you can use any condition according to your requirement.

Complete Code

<!DOCTYPE html>
<html>
<head>
    <title>AddClass JQuery Demo - Sibeesh Passion</title>
    <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>
    <script>
        $(document).ready(function () {
            setTimeout(function () {
				$('.first').addClass('second');
			}, 5000);
        });
    </script>
    <style>
        .first {
            width: 100px;
			height: 100px;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            margin: 10px;
            box-shadow: 1px 1px 1px #999;
            font-style: oblique;
            text-align: center;
			background:green;
        }
		.second {
            width: 200px;
			height: 200px;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            margin: 10px;
            box-shadow: 1px 1px 1px #999;
            background: blue;
            font-style: oblique;
            text-align: center;
        }
    </style>
</head>
<body>
    AddClass JQuery Demo - Sibeesh Passion
	<div class="first"></div>
</body>
</html>

Conclusion

Did I miss anything that you may think which is needed? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Kindest Regards
Sibeesh Venu

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

 
GeneralFundamental information missing. Pin
wvdhouten22-Oct-15 21:29
wvdhouten22-Oct-15 21:29 
You have a nice pragmatic approach to solving solutions, and writing articles about your findings often helps not only others understand what you have written, but also should help yourself understand why something works the way it does.

I am firmly believing one only understands a technology when they can explain it to somebody else.

What I find missing in your article is a fundamental understanding of why this code does the things it does.

Adding a class to an element in order to make it resolve it appearance differently is nice, but not explaining it's pitfalls might leave people puzzled. A deeper understanding of how CSS is resolved would help people understand how to create solutions, rather than find some on the internet and hope for the best.

In this case, it is important to know that CSS resolves according to specicifity. More weight is applied to a selector, the more important it's properties will be. (Given that !important always takes priority) if the specicifity is equal, it will look at the latest definition.

If '.second' would have been defined before '.first', your code would not have worked.

Keep up the work though!
GeneralRe: Fundamental information missing. Pin
ahagel27-Oct-15 20:14
professionalahagel27-Oct-15 20:14 
GeneralRe: Fundamental information missing. Pin
wvdhouten6-Nov-15 0:58
wvdhouten6-Nov-15 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.