1

Im trying to disable a button and action link when the action link is pressed. I have tried everything online and nothing seams to work any suggestions?

my html code:

<button type="button" id="cancel" class="btn btn-default" data-dismiss="modal">Cancel</button>
@Html.ActionLink("Yes", "RemoveHandler", new { path = x }, new { onclick = "disable()" }) 

js:

function disable() {
    document.getElementById("cancel").disabled = true;
    document.getElementById("ok").disabled = true;
}
shanizoe
  • 121
  • 1
  • 2
  • 5

2 Answers2

1

MVC razor is generating <a> tag from @Html.ActionLink

There is no html attribute to disable <a> tag. You have to remove it.

Although you can remove href and it will be same thing as disabling. Or you can set href to "#" or "javascript:void(0)"

Try This

function disable() { document.getElementById("mylink").href = "javascript:void(0)"; }

Put id in ActionLink

@Html.ActionLink("Yes", "RemoveHandler", new { path = "asdf" }, new { onclick = "disable()" , id="mylink"})

Savan S
  • 407
  • 4
  • 19
0

To explain the problem, when you click on the button, your javascript code executes and then your action link sends the page and in the new page the button is not disabled.

You can try this in your javascript method :

function disable() {
document.getElementById("cancel").disabled = true;
document.getElementById("ok").disabled = true;

return false;
}

and in your html :

@Html.ActionLink("Yes", "RemoveHandler", new { path = x }, new { onclick = "return disable()" })

Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32