How to detect and change the content/style of a div using jQuery ?
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.
<!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> |
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.
<!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> |
Output:
- Before clicking the div element:

- After clicking the div element:

Recommended Posts:
- How to detect value change on hidden input field in JQuery ?
- How to detect the change in DIV's dimension ?
- How to detect a route change in AngularJS ?
- How to detect escape key press using jQuery?
- How to detect a mobile device in jQuery?
- How to detect the user's device using jQuery ?
- JQuery | Detect a textbox content is changed or not
- How to change the checkbox value using jQuery ?
- jQuery | change() with Examples
- How to change the element id using jQuery ?
- How to change cursor style using jQuery ?
- How to change the background image using jQuery ?
- How to Change the Text of a Button using jQuery?
- How to change an HTML element name using jQuery ?
- How to change the placeholder text using jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


