Link to home
Start Free TrialLog in
Avatar of ddantes
ddantesFlag for United States of America

asked on

Activate a disabled button with javascript

A submit button on a web page is disabled by default.  I would like to enable it if the visitor has javascript.  The code below will accomplish what I wish, except for the <body onLoad> code,  which somehow interferes with that.   Please advise how I can accomplish this intention, while preserving the <body onLoad> scrolling.

<!doctype html>
<html>
<head>
</head>	
<body onLoad="window.parent.parent.scrollTo(0,0)">
<br><br><br><br>
<button type="submit" id="submit" disabled class="btn100">Submit Form</button>

<script type="text/javascript">
var _onload = window.onload || function()
{
  document.getElementById('submit').disabled = false;
}
_onload();
</script>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ddantes

ASKER

Scott: that worked perfectly.  Would you mind explaining how?   I noticed that you removed the onLoad scroll code, but that is necessary.  If I replace that onLoad code, the button is still enabled.
First of all, I probably would have used jquery, but that is me.  I am not a js purist.  

In simple terms, the window is the browser or at least the current tab you are on and the onLoad event waits until the DOM is ready.   There is a function the code above called "load" and using  window.onload = load; fires that function.  

Let's say you have multiple functions that need to fire when the window loads, then you can do something like this.
<!doctype html>
<html>
<head>
</head>	
<body>

<button type="submit" id="submit" disabled class="btn100">Submit Form</button>

<script>
      function load() {
        foo();
        var();
      }
      function foo() {
        alert("foo");
      }
      function bar() {
        alert("bar");
      }
      window.onload = load;
    </script>
</body>
</html>

Open in new window


If you want to read details, http://www.whatwg.org/specs/web-apps/current-work/#handler-onload or http://www.w3.org/TR/html401/interact/scripts.html

I prefer https://developer.mozilla.org/en-US/docs/Web/API/window or  https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload

In jquery it would be either
$(function(){
    // do something
});

Open in new window

which is short hand for
$( document ).ready(function() {
    // do something
});

Open in new window

Avatar of ddantes

ASKER

Thank you for the explanation and article links.  There seems to be a little issue, about which I'd appreciate your feedback.  The onLoad function scrolls the page to the top when it is loaded.  This is needed, because the page which contains this code is a frameset child.  Without that onLoad, if the page is loaded by a link which appears toward the bottom of another child page, then the page also loads toward the bottom.  Since I implemented the code which you supplied, it appears that the onLoad scroll to top doesn't work.  Any thoughts?
I didn't know what that was for so I didn't use it.  Just add it back.  Or place it in the load function.
Avatar of ddantes

ASKER

I added it back, but it isn't working, unless I remove your javascript.  In other words, the page doesn't load at the top if your script is included.  It does load at the top without that script, but then the submit button is disabled.
Start a new question, and use the sample code to recreate the issue. I can't tell from what is here.