views:

1927

answers:

6

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

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

please,suggest some answers if you have

i tried with the below samples but not working

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

and

$("a.add").hide();
+3  A: 
$('#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

rahul
If you define it like that you cannot unbind it easily if you wan't to restore default behavior later.
RaYell
+5  A: 
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);
RaYell
+1  A: 

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).

cpharmston
+1  A: 

Actually My coding is in Rails and my coding is

<%= foo.add_associated_link('Add email', @project.email.build) %>


when it I render it into browser i can see the email but i cannot disabled it even i tried with coding such as

e.preventDefault()


but of no result

Senthil Kumar Bhaskaran
A: 

(Apologies for writing another answer and not commenting; I'm too much of a noob to comment)

Would you mind posting the rendered source that the jQuery is interacting with? My guess is that there's an errant jQuery selector. It'd be much easier to construct a correct selector if we can see the HTML that we're working with rather than the Rails code that renders it.

cpharmston
A: 

Thank you Ra Yell it's working

nirav