codeGeass codeGeass - 1 year ago 93
jQuery Question

How to make a jQuery event only once?

When I click on "DELETE", the function

is called. How can I prepend only once, even if I click on DELETE many times? I want it to happen just the first time I click.

function deleting() {
$(".checking").prepend("<input type='checkbox' name='idcheckbox'>");

<script src=""></script>
<a href="javascript:deleting()">DELETE</a>
<div class="checking"></div>

Answer Source

You may use jQuery one in order to: Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

You may attach the event dynamically and not inline:

    $(function () {
        $('a').one('click', function(e) {
            $(".checking").prepend("<input type='checkbox' name='idcheckbox'>");

And you html could be like:

 <a href="">DELETE</a>