Hello Readers,
on() is the jQuery method and it comes in jQuery 1.7 and it is the replacement of bind() and delegate() handlers methods and it is used to attach event to the currently selected elements and it is a faster way to attach to the event handler.
Syntax :
$(elements).on(events [, selector] [, data], handler);
selector : It is optional parameter and it is a Selector String
data : it is also a optional parameter and it is any data to pass to the handler when the event is triggered
handler : when the event is triggered handler function execute.
Code Example :
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$('div').on('click', function( event ){
alert('Hi there');
});
});
</script>
<style>
.div{ margin:10px;padding:12px; border:2px solid #666; width:60px;}
</style>
</head>
<body>
<p>Click</p>
<div class = "div" style = "background-color:blue;"></div>
</body>
</html>
In the above code, we use the on() method and take the div as a selector and on click execute the event handler function and give the alert message.
0 Comment(s)