0

I use the following button.

<input type="button" id="attractionsSection" value="Warsztaty" onclick="location.href='@Url.Action("AddAttractions","ReservationDetails")'" disabled="disabled" />

In js script i check wheather some condition is true and then I set property disabled of button.

if(Model.stagesConfirmation[4]==true)
                                {
                                <script>
                                    $(function () {
                                        console.log('Test1');
                                        document.getElementById('attractionsSection').disabled = 'true';
                                        console.log(document.getElementById('attractionsSection'));
                                        console.log('Test2');
                                    });
                                </script>
                            }

I use console log to test wheather this condition is passed and that is OK. I set disabled and button still remains active. This console.log

console.log(document.getElementById('attractionsSection'));

shows me the following thing

<input type="button" id="attractionsSection" value="Warsztaty" onclick="location.href='@Url.Action("AddAttractions","ReservationDetails")'"/>

So it is not disabled. How to solve, what can be the problem?

maciejka
  • 818
  • 2
  • 17
  • 42
  • Do you see that output in your browser console? E.g. "Test1", "Test2", etc? Are you sure your button is rendered by the time any of that executes? – Marc Sep 12 '17 at 21:40
  • Yes Test1 and Test2 shows me in browser. – maciejka Sep 12 '17 at 21:40
  • Okay, but are you enforcing that the page is rendered before your script runs? Doesn't look like it. And the button appears to be disabled by default, so how would you know if your script is disabling the button or not? Look into various methods (jQuery and others) of waiting for the page to load before executing your script. – Marc Sep 12 '17 at 21:41

1 Answers1

0

See this answer. You need to use element.removeAttribute("disabled"); or element.disabled = false;

You don't say disabled=true you just say disabled. Check here for documentation on the disabled attribute.

You could say disabled="foobar" and it will still disable the button. To prove that, play around with this.

swagrov
  • 1,510
  • 3
  • 22
  • 38
  • On the same page I use document.getElementById('attractionsSection').disabled = 'true'; for other buttons and it works fine and this buttons have the same structure. – maciejka Sep 12 '17 at 21:53
  • Okay, relevant tryit here https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_pushbutton_disabled2 I am not able to reproduce your issue here. What happens if you `console.log(document.getElementById('attractionsSection'));` *before* setting `disabled=true`? – swagrov Sep 12 '17 at 22:03
  • Problem was in other place I have overriden attribute disabled of this button on false that;t why it didn't work. Thanks for advice. – maciejka Sep 12 '17 at 22:03