Tutorial :jquery link tag enable disable



Question:

I want to disable the link during loading, for the code given below

<span id="addlink">"<%= f.add_associated_link('Add Task', @project.tasks.build, :class=>"add") %></span>  

I tried it with the codes below but it didn't work

$("#addlink").attr("disabled", "disabled");   

and

$("a.add").hide();  


Solution:1

function disableLink(e) {      // cancels the event      e.preventDefault();        return false;  }  

When you want to disable it yo call

$('#addlink').bind('click', disableLink);  

When you want to enable disabled link you call

$('#addlink').unbind('click', disableLink);  


Solution:2

$('#addlink').click(function(e) {      e.preventDefault();      //do other stuff when a click happens  });  

return false;

will prevent the default event from occuring and and also prevent the event from bubbling up

So chosing between these two depends on your use. If you want to stop the default action and also need to bubble up the event then use preventDefault


Solution:3

I'd go with a hybrid of RaYell's and phoenix's solution, adding jQuery's namespacing to the mix:

$('#addlink').bind('click.killlink',function(event){      event.preventDefault();      // You can do any additional onClick behavior here  });  

To unbind this event, as well as any other related events (of any type) that you group with the .killink namespace, you'd run this:

$('#addlink').unbind('.killlink');  

As phoenix pointed out, using return false will prevent the event from bubbling up. preventDefault() has the added benefit of being extremely explicit (unlike return false, which can mean many different things depending on the context).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »