Tauseef Hussain Tauseef Hussain - 4 years ago 132
Javascript Question

Jquery: Append div only once 'on click'

I am trying to append a

section
to a
div
on the click of a button. Ideally I want the
section
to be appended only on the first click, the code that I use below appends a
section
everytime I click the
div
. How should I go about this?

$("#nextractorapply").click(function () {
$("#main").append('<section id="nextractor" class="five"> </section>');
});

Answer Source

You could use one(), which fires only once

$("#nextractorapply").one('click', function () { 
    // executes only once

     $("#main").append('<section id="nextractor" class="five"> </section>');  
});

$("#nextractorapply").on('click', function () { 
    // executes every time

    // do other stuff
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download