Monday, August 8, 2016

jQuery - when bind-first plugin is not enough to disable a button

If you want to conditionaly disable a button, without knowing the handlers attached to it, this code might interest you:

jQuery(".add_to_cart_button").liveFirst("mouseup",function(e){


//jQuery("form[name=checkout]").liveFirst("submit",function(e){

e.preventDefault();
e.stopPropagation();

if(window.wcg_settings.cart_empty==0)
{

//jQuery(this).off();

jQuery(this).attr("disabled",true);

var resp=confirm("This action will empty the contents of your current cart. Are you sure you want to proceed?");
window.resp3=resp;

if(resp){

jQuery(this).attr("disabled",false);
return true;

}

return resp;

}
else{

jQuery(this).attr("disabled", false);
return true;
}


});

Basically, we attach to mouseup, which happens before "click" event, and conditionally disable the button!
Also, if it doesn't work, you can add it inside a setTimeout block, so you make sure it's not being overwritten by some other JS code.

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...