All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Add CSS Using jQuery

Last Updated : Mar 11, 2024

Add CSS Using jQuery

In this article we will show you the solution of add CSS using jQuery, here we needs to use css() and click() methods. The css() method sets or returns one or more style properties for the selected elements.

It returns the specified CSS property value of the FIRST matched element. The click() method simulates a mouse-click on an element.

This method can be used to execute a click on an element as if the user manually clicked on it.

Step By Step Guide On Add CSS Using jQuery :-

Here we defined div element with id attribute and button ‘Add CSS’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button tag then there we referring div element by id ‘cir’ which is appends with css() method with ‘background-color’ property sets to green color when user clicks on button ‘Add CSS’.

<!DOCTYPE html>
<html>
    <head>
        <title>Add Css</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
        <script>
            $(document).ready(function(){
                $('button').click(function(){
                    $('#cir').css('background-color','green');
                })
            })
        </script>
        </head>
        <body>
            <style>
                #cir{
                    width: 50px;
                    height: 50px;
                    background-color: purple;
                    border-radius: 50%;
                }
            </style>
            <div id="cir"></div>
            <button>Add CSS</button>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we appending click() method with button element by using tag name ‘button’ for generate click event when user clicks ‘Add CSS’.
  8. Within function we pointing div element by id ‘cir’ and this element appends with css() method. Which is have ‘background-color’-refers style property name, 'green'-refers style property value as arguments for sets dynamic value instead of defined style value.
  9. In html block we defined div elements with id attribute ‘cir’. The id ‘cir’ is defined with some styles i.e (width and height sets to 50px, border-radius sets to 50% and background color sets to purple) thats way the div element displayed in circle shape.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to add css using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see purple circle and button ‘Add CSS’ now user needs to click on button we can see circle color changed to green color.

I hope this article on add CSS using jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪