Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery how to untrigger a function

I'm creating a dynamic form. I have a dropdown list which when the user makes a selection, the database is queried to see if the item required privacy or not. If it doesn't then additional fields are added to the form. Also a zip code lookup ajax function is triggered, but should only be triggered if the item is not private.

This is all working fine, until a user changes their mind...

Here is the relevant part of my code that runs when ever an item is selected from the dropdown;

success:function(response){
    if(response === '1'){
        // do stuff for item that requires privacy
        // do NOT trigger zip_check function
    }else if(response === '0'){
        // do stuff for item that is public +
        $('#zip-code').keyup(zip_check); // triggers a function to check zip code
    }
}

This works as expected until a user selects a non private item then changes their mind and selects a private item in which case the zip_check function will still run even though it now shouldn't be getting triggered. Where am I going wrong?

like image 701
Ally Avatar asked Oct 07 '13 11:10

Ally


1 Answers

try this:

for binding:

$('#zip-code').on('keyup',zip_check);

and for unbinding:

$('#zip-code').off('keyup',zip_check);
like image 160
Harpreet Singh Avatar answered Sep 20 '22 15:09

Harpreet Singh