Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my JS not enabling my button?

Tags:

javascript

I have a form that needs JavaScript in order for a user to submit it. When my HTML is output, the disabled attribute is set to true. I want my javascript to enable the button. My JS is not enabling my button for some reason. I get no errors in the console.

The JS:

(function(){
    var buttons = document.getElementsByTagName("button");
    buttons[0].disabled = false;
});

The HTML:

<button type="submit" class="btn btn-primary" disabled="true">Next</button>

I have also tried DISABLED in the HTML instead of disabled="true".

like image 692
ShoeLace1291 Avatar asked Dec 25 '22 08:12

ShoeLace1291


1 Answers

Try to call the function, You are simply creating an anonymous function and not calling it at all.

(function(){
    var buttons = document.getElementsByTagName("button");
    buttons[0].disabled = false;
})();
//-^

Also the placement of this code is also important. Either Place the code at the end of body tag or use an onload event.

like image 186
Rajaprabhu Aravindasamy Avatar answered Dec 28 '22 22:12

Rajaprabhu Aravindasamy