Overwrite CSS Styles Using addClass In jQuery

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 stylesheet manually. But what if you want to do the same dynamically? If you need to change a CSS property dynamically, I suggest you to read it here. 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 elements. 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.

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>AddClass JQuery Demo - Sibeesh Passion</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     AddClass JQuery Demo - Sibeesh Passion  
  10. </body>  
  11.   
  12. </html>  
Then you need to include the script needed.
  1. <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>   
Now you need to write the scripts.
  1. <script>  
  2. $(document).ready(function()  
  3. {  
  4.     setTimeout(function()  
  5.     {  
  6.         $('.first').addClass('second');  
  7.     }, 5000);  
  8. });  
  9. </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.
  1. <style>  
  2. .first {  
  3.     width100px;  
  4.     height100px;  
  5.     border1px solid #ccc;  
  6.     border-radius: 5px;  
  7.     padding10px;  
  8.     margin10px;  
  9.     box-shadow: 1px 1px 1px #999;  
  10.     font-styleoblique;  
  11.     text-aligncenter;  
  12.     backgroundgreen;  
  13. }  
  14.   
  15. .second {  
  16.     width200px;  
  17.     height200px;  
  18.     border1px solid #ccc;  
  19.     border-radius: 5px;  
  20.     padding10px;  
  21.     margin10px;  
  22.     box-shadow: 1px 1px 1px #999;  
  23.     backgroundblue;  
  24.     font-styleoblique;  
  25.     text-aligncenter;  
  26. }  
  27. </style>  
Now please run your browser, you can see the following output.

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

output

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

Add class JQuery

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
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>AddClass JQuery Demo - Sibeesh Passion</title>  
  6.     <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>  
  7.     <script>  
  8.     $(document).ready(function()  
  9.     {  
  10.         setTimeout(function()  
  11.         {  
  12.             $('.first').addClass('second');  
  13.         }, 5000);  
  14.     });  
  15.     </script>  
  16.     <style>  
  17.     .first {  
  18.         width100px;  
  19.         height100px;  
  20.         border1px solid #ccc;  
  21.         border-radius: 5px;  
  22.         padding10px;  
  23.         margin10px;  
  24.         box-shadow: 1px 1px 1px #999;  
  25.         font-styleoblique;  
  26.         text-aligncenter;  
  27.         backgroundgreen;  
  28.     }  
  29.       
  30.     .second {  
  31.         width200px;  
  32.         height200px;  
  33.         border1px solid #ccc;  
  34.         border-radius: 5px;  
  35.         padding10px;  
  36.         margin10px;  
  37.         box-shadow: 1px 1px 1px #999;  
  38.         backgroundblue;  
  39.         font-styleoblique;  
  40.         text-aligncenter;  
  41.     }  
  42.     </style>  
  43. </head>  
  44.   
  45. <body>  
  46.     AddClass JQuery Demo - Sibeesh Passion  
  47.     <div class="first"></div>  
  48. </body>  
  49.   
  50. </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 auseful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

If you have any questions, then please mention it in the comments section.

Read this article in my blog here.