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:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Nov, 2019
Like Article
Save Article