Search code examples
javascriptc#asp.net-mvcrazor

Class added in button with razor code I have a problem when I click the button


I have a table in my browser with different rows I would like to enable and disable the buttons when a condition is true. I add in the button a class .disable. This is the code for the button:

<button type="button" id="mybtn" class='btn @(value != "" ? "disabled" : "")' 
    onclick='myfunction()'>

I would like to show at the users that when they click on disable button they see an alert "Disabled".

This is the code of my function

function myfunction() {
    if($("#mybtn").is(".disabled")) {
        alert("disabled");
    } 
    else {
        alert("enabled");
    }
}

When I click the disabled button the browser shows me the enabled alert.

Can someone help me?


Solution

  • I suppose your approach should be a little different. If you want to make the button enabled/disabled depend on the value it's necessary to operate with the disabled attribute, not the class. Try the following:

    <button type="button" id="mybtn" @(value != "" ? "disabled" : "") onclick='myfunction()'>