Solved

Activate a disabled button with javascript

Posted on 2014-07-20
8
1,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 53

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 53

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
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!

 

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
 
LVL 53

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 53

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

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

707 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