What is difference between click and onclick in jquery
What is difference between click and onclick in jquery
I would prefer .on over .click because the former can use less memory and work for dynamically added elements, A simple difference between click and onclick in jquery.
.
<html>
<button id="add">Add new</button>
<div id="container">
<button class="alert">alert!</button>
</div>
</html>
where we add new buttons via -
<script>
$("button#add").click(function() {
var html = "<button class='alert'>Alert!</button>";
$("button.alert:last").parent().append(html);
});
// click
$("button.alert").click(function() {
alert(1);
});
// on click
$("div#container").on('click', 'button.alert', function() {
alert(1);
});
</script>
'.click'
Clicks for clicks that remain static data.
'.on click'
On click We have to click on any of the data that we have appended to, so that we can not click through the clicks, we will have to on click them.
Follow On Social Media -
Follow on Facebook - https://www.facebook.com/Front-End-Issue-487743404963344
Follow on instagram - https://www.instagram.com/frontendissue/
Follow on Twitter - https://twitter.com/IssueEnd
Follow on Linkedin - https://www.linkedin.com/in/hitesh-patidar-34253a10a/
Follow on GooglePlus - https://plus.google.com/118238268171156252992
Follow on pinterest - https://in.pinterest.com/frontendissue/
Comments
Post a Comment