0

I have some buttons, and in javascript I do some manipulation. Depending on what the user clicks, buttons will be enabled and disabled.

The logic and the enabling/disabling works well. But my css isn't updating the look of a disabled button. I think my question is, should it automatically update? Or do I need to handle that somehow?

I'm using ASP.NET MVC if that is relevant.

I simply added this to the top of my view (for testing) :

<style>
   button[disabled] {
       background: green;
   }
</style>

and a snippet of the javascript :

function changeNameEnabled(enabled) {
    document.getElementsByName("FullNameExact")[0].disabled = !enabled;
}

a sample button:

<button onclick="nameClick(this)" name="FullNameExact" style="height:20%;width:75%;top:5%;left:13%;margin-bottom:5%">FULL NAME EXACT</button>

and I can see in the browser that the button gains the disabled attribute, and is unclickable

tereško
  • 58,060
  • 25
  • 98
  • 150
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112

3 Answers3

0

Why not add a class to the button when disabling it? The CSS will automatically be applied when the class is added.

document.getElementById("MyElement").className = "MyClass";

In general, for managing the classes in javascript: Change an element's class with JavaScript

Community
  • 1
  • 1
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
0

I found what was wrong. Have a look at this jsFiddle: http://jsfiddle.net/6jht2/1/

I made your code usable in a fiddle. What you were doing is passing an DOM element to a function by using this in an onclick attribute:

<button onclick="nameClick(this)" name="FullNameExact" style="height:20%;width:75%;top:5%;left:13%;margin-bottom:5%">FULL NAME EXACT</button>

So when the function is called, it will try to invert a DOM element. And error.

Even if you set the disabled attribute to a boolean it won't work. You will need to use toString on a boolean or any string, even 'a' or 'foo'.

And here everything works. Hope it'll help.

Tip: don't use the onclick attribute. Instead, use the DOM event handling.

Yohaï-Eliel Berreby
  • 1,165
  • 3
  • 14
  • 25
-3

button[disabled] wont work , better add some .disabled class , then it will work out.

Vladimir
  • 377
  • 1
  • 6