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

ddantesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
http://jsbin.com/qecoj/1/edit
<!doctype html>
<html>
<head>
</head>	
<body>

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

<script>
      function load() {
        document.getElementById('submit').disabled = false;
        //alert("load event detected!");
      }
      window.onload = load;
    </script>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddantesAuthor Commented:
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.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ddantesAuthor Commented:
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?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.
0
ddantesAuthor Commented:
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.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Start a new question, and use the sample code to recreate the issue. I can't tell from what is here.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.