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.

What is difference between click and onclick in jquery


















Follow On Social Media -

Follow on instagram   -  https://www.instagram.com/frontendissue/  
Follow on Twitter       -  https://twitter.com/IssueEnd
Follow on pinterest    -  https://in.pinterest.com/frontendissue/


Comments

Popular posts from this blog

Owl carousel center mode with zoom image

Add (+/-) Button Number Incrementers using jQuery