AnApprentice
AnApprentice

Reputation: 110960

jQuery Submit Function - How not to have it binded if already binded

I have the following:

$('#xxxxx').submit(function() {
....
});

This is contained within a script that gets called w jQuery getScript... How can I make sure this bind only gets applied once? And or, clear out beforehand?

Thanks

Upvotes: 0

Views: 82

Answers (3)

sv_in
sv_in

Reputation: 14049

Add a class to the form when attaching the handler. When attaching again, just check if the class is already added to the form.

var $xxxx = $('#xxxx')
if (!$xxxx.hasClass('handleradded')) {
  $xxxx.submit(function(){
      ....
  }).addClass('handleradded');
}

Solutions given by others are good ones. I just wanted to give you an alternative. :)

Upvotes: 0

Vlad Khomich
Vlad Khomich

Reputation: 5880

you can use 'unbind' :

$('#xxxxx').unbind('click').
    bind('click', function() {
         ....
    })

Upvotes: 1

alex
alex

Reputation: 490283

$('#xxxxx').unbind('submit').submit(function() {
....
});

Upvotes: 1

Related Questions