0

I've tried everything:

document.getElementById('buy').setAttribute('disabled','false');
document.getElementById('buy').setAttribute('disabled',false);
document.getElementById('buy').removeAttribute('disabled');
document.getElementById('buy').disabled=false;

The input submit is has previously been echoed by php:

if (empty($basketproductos)) {
echo "<input type='submit' name='buy' value='Buy' disabled='disabled' id='buy'/>";
            }

Also tried with disabled='true' instead of disabled='disabled'.

Rajo
  • 31
  • 6
  • 1
    Make sure the element is added to the DOM before you execute any of those statements. – Titus May 21 '18 at 16:16
  • How can I check that? – Rajo May 21 '18 at 16:21
  • You can see an example of that [HERE](https://stackoverflow.com/a/800010/1552587) – Titus May 21 '18 at 16:23
  • Hmmm but as far as I see that's about how to document.ready, onload, etc, but my js files are loaded after my HTML and anyways the dis-disabling function comes from a lately clicked button... – Rajo May 21 '18 at 16:33
  • I see, in that case, the problem is something else because some of those statements should have enabled the input. Take a look at your browser's console to see if there are any errors. – Titus May 21 '18 at 16:36
  • Any errors... I'm just flushing and regenerating the entire basket in JS as a temporarily solution, if anyone as a new idea... – Rajo May 21 '18 at 16:44

2 Answers2

0

If you want to disable the button, you should set true for disable property.

document.getElementById('buy').disabled = true;

And like @Titus said (on the comment), make sure the element is added to the DOM before you execute a statement.

Felipe Oriani
  • 35,246
  • 17
  • 121
  • 176
0

Maybe your js is executed before the html, this should work:

document.addEventListener('DOMContentLoaded', function(){

  var buyButton = document.getElementById('buy');
buyButton.disabled = false; //or true

}, false);

or

<form action="">
<input type='submit' name='buy' value='Buy' disabled='false' id='buy'/>
  </form>
  <script>    
  var buyButton = document.getElementById('buy');
buyButton.disabled = false; //or true
  </script>

Functional example here: https://jsfiddle.net/cfcdh99n/

Emeeus
  • 4,104
  • 2
  • 14
  • 32