Solved

Activate a disabled button with javascript

Posted on 2014-07-20
8
1,150 Views
Last Modified: 2014-07-21
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
Comment
Question by:ddantes
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
ID: 40208216
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
 

Author Comment

by:ddantes
ID: 40208226
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40208241
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
 

Author Comment

by:ddantes
ID: 40208252
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40208314
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
 

Author Comment

by:ddantes
ID: 40208339
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40208372
Start a new question, and use the sample code to recreate the issue. I can't tell from what is here.
0
 

Author Comment

by:ddantes
ID: 40208431
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now