Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1549
  • Last Modified:

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

0
ddantes
Asked:
ddantes
  • 4
  • 4
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now