2

Using Onsen 2.0 with Monaca, I am having an issue setting the disable property to true via JavaScript and not Angular. I am aware of how to do this with AngularJS and binding to a variable, but I just need to set the property via straight JS as this project is not using AngularJS at all. I fear this may not be possible.

Here is the code which does not work:

<ons-button id="btn">Stop</ons-button>
<ons-button onclick="document.getElementById('myBtn').disabled = true;">Go</ons-button>

Edit: Huge thank you to Fran again for solving my issue. For others, since this is simply setting the CSS, to disable the button:

document.getElementById("myButton").setAttribute('disabled', '');

and to re-enable it after using setAttribute:

document.getElementById("myButton").removeAttribute('disabled', '');
Munsterlander
  • 1,356
  • 1
  • 16
  • 29

1 Answers1

2

disabled is not an internal variable, it's just an attribute used to apply some CSS. Try with document.getElementById('myBtn').setAttribute('disabled', '').

Fran Dios
  • 3,482
  • 2
  • 15
  • 26
  • Thank you. Coming from a different language background has been confusing at times. Especially, in regards to the help docs, such as: https://onsen.io/2/reference/ons-button.html. Disabled is listed as an attribute with modifier, so I think of them as internal variables when in reality the attribute would be more like CSS classes (at least for button). It becomes even more confusing in the docs, with a declared method of setDisabled(): https://onsen.io/2/reference/ons-button.html#method-setDisabled – Munsterlander Jan 31 '16 at 15:36
  • 1
    @Munsterlander `disabled` is in the category "Attributes" in that link, so you can use `setAttribute`. It's true that there was a method `setDisabled` that internally just called `setAttribute`, but I think it's now removed from the beta. We still need to update the docs though. – Fran Dios Jan 31 '16 at 16:31
  • Well that would make sense then @Fran-dios, ha! I never thought about it like that, Attributes = setAttribute. This will make a huge difference going forward. Thanks again, like always! – Munsterlander Jan 31 '16 at 17:58