ar.gorgin
ar.gorgin

Reputation: 5012

How do Enable/Disable linkbutton in javascript?

I have a link button in the page like:

<asp:LinkButton ID="edit" runat="server" OnClick="edit_Click" Enabled="False">ویرایش</asp:LinkButton>

I want Enable/Disable this in javascript.

I use this code but set visible

var objedit = document.getElementById('<%= edit.ClientID.ToString() %>');
objedit.style.display = "none";

I use this code but not enable

if (count == 1) {
    objedit.disabled = false;
} else {
    objedit.disabled = true;
}

I can click but link button is disabled.

enter image description here

Upvotes: 5

Views: 17208

Answers (5)

Moulika Pinapathruni
Moulika Pinapathruni

Reputation: 31

document.getElementById("lnk").style.display = "none";

Upvotes: 0

Alex Kudryashev
Alex Kudryashev

Reputation: 9470

To disable element:

document.getElementById('<%# edit.ClientID %>').disabled = 'disabled';
//.ToString() is not necessary; ClientID is a string.

To re-enable:

document.getElementById('<%# edit.ClientID %>').disabled = '';

Of course, it can be done after document (DOM) is loaded.

Upvotes: 1

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94339

So, is this what you want?
http://jsfiddle.net/hzaR6/
http://jsfiddle.net/hzaR6/2/ -- UPDATED, tested in Chrome and Firefox

The UPDATED way

You can use a class name to define disabled element, for which you can have more control on their styles ... etc.

$("#link").toggleClass("disabled"); //This will simply toggle the class

and for the css

#link.disabled{
    z-index:-1;            /*Make it not clickable*/
    position:relative;
    opacity: .5;           /*Lighter*/
}​

You can do whatever you want here.

The good old form element way

$("#edit").attr("disabled", false);
 -or-
document.getElementBy("edit").disabled = false;

This will disable any form element. If you want to enable them, just change false to true.


var a = document.getElementBy("edit").disabled;

a will be true if the element is disabled. Otherwise it will be false.

Upvotes: 1

Daniel Szabo
Daniel Szabo

Reputation: 7281

This link should give you everything you need. You can't really "disable" a linkbutton because its just a link with some javascript attached. Basically, you need to reassign the click handler to something that returns void or false.

You can refer to the ID of the link with the following script:

<script runat="server">
   var a = document.getElementById('<%= edit.ClientID.ToString() %>')
</script>

Upvotes: 1

Peter Wateber
Peter Wateber

Reputation: 3948

try this

var objedit = document.getElementById("edit"); //not editid (editid is a variable)
objedit.disabled = true; //if I'm not mistaken its true/false or disabled

Upvotes: 0

Related Questions