javascript - How to preventDefault and disable a href -


on first click button click function works, upon clicking again refreshes page click unbind isn't recognized. need prevent default click event disable button after click.

<a href="" class="red-btn2">save property</a>  $(".red-btn2").click(function(event){   event.preventdefault();   $(this).unbind('click');   $(this).css({     'background-color':'#666666',     'text-align':'center'   });   $(this).text("added favorites"); }); 

if need rest of handler run once, preventdefault() always, can separate them multiple handlers.

then, 1 of them can removed after first use (using jquery's .one() cover you), still leaving preventdefault() in use:

$('.red-btn2')     .click(function (event) {         event.preventdefault();     })     .one('click', function () {       $(this).css({         'background-color':'#666666',         'text-align':'center'       });       $(this).text("added favorites");     }); 

as have it, event.preventdefault() being removed .unbind('click'), no functions left on element call 2nd click.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -