Related Articles

Related Articles

How to detect and change the content/style of a div using jQuery ?
  • Last Updated : 05 Nov, 2019

The changes to the html/text of a div can be detected using jQuery on() method. The on() is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The on() method is the replacement of the bind(), live() and delegate() method from the jQuery version 1.7.

Syntax:

$(selector).on(event, childSelector, data, function, map)

Example 1: This example uses jQuery on() method to change the text of the <div> element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to detect and change the
        content of a div using JQuery ?
    </title>
      
    <script src=
    </script>
      
    <style>
        #div1 { 
            font-size: 35px; 
            color:green;
            text-align:center;
        
    </style>
      
    <script>
        $(document).ready(function(){
            $("div").on("click", function(){
                document.getElementById("div1").innerHTML
                    = "A computer science portal for geeks"; 
            });
        });
    </script>
</head>
  
<body>
    <div id="div1">GeeksforGeeks!</div>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the div element:
  • After clicking the div element:

Example 2: This example uses jQuery on() method to change the style of a <div> element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to detect and change the
        style of a div using JQuery?
    </title>
      
    <script src=
    </script>
      
    <style>
        #div1 { 
            font-size: 35px; 
            color:green;
            text-align:center;
        
    </style>
      
    <script>
        $(document).ready(function(){
            $("div").on("click", function(){
                document.getElementById("div1").style.color
                            = "white"
                              
                document.getElementById("div1").style.backgroundColor
                            = "green"
            });
        });
    </script>
</head>
  
<body>
    <div id="div1">Welcome to GeeksforGeeks!!!</div>
 </body>
  
</html>

chevron_right


Output:

  • Before clicking the div element:
  • After clicking the div element:



My Personal Notes arrow_drop_up
Recommended Articles
Page :