tags:

views:

409

answers:

7

I need the button "submit" to be disabled unless JavaScript is on.

I tried:

1.

<input onLoad="this.disabled=false" id="post-comment" type="submit" 
value="Post Your Comment" disabled="disabled"/>

2.

<input onLoad="this.removeAttribute('disabled');" id="post-comment" type="submit"
value="Post Your Comment" disabled="disabled"/>

3.

<input onLoad="document.getElementById('post-comment').removeAttribute('disabled');"
id="post-comment" type="submit" value="Post Your Comment" disabled="disabled"/>

Doesn't work. I'm new to JavaScript, but can't find answer on the net.

A: 

You can force the submit button to call a javascript function that actually does the submit.

Kevin Crowell
A: 

The onload parameter to the submit element is a Javascript event handler. Therefore, if the user does not have Javascript turned on, this event will not fire.

I suggest the following:

Instead of having a submit button in the page, have an image which is of a greyed out button. Then, do something like this:

<img onLoad="enableSubmit()" .../>

The onLoad event handler will only fire if Javascript is enabled. In your enableSubmit() function (defined elsewhere), you can replace the img tag with an input type="submit".

Mike Pollitt
+3  A: 

The problem is that input elements don't have an "onload" event. The spec shows the available events as:

  onfocus     %Script;       #IMPLIED  -- the element got the focus --
  onblur      %Script;       #IMPLIED  -- the element lost the focus --
  onselect    %Script;       #IMPLIED  -- some text was selected --
  onchange    %Script;       #IMPLIED  -- the element value was changed --
  onclick     %Script;       #IMPLIED  -- a pointer button was clicked --
  ondblclick  %Script;       #IMPLIED  -- a pointer button was double clicked--
  onmousedown %Script;       #IMPLIED  -- a pointer button was pressed down --
  onmouseup   %Script;       #IMPLIED  -- a pointer button was released --
  onmouseover %Script;       #IMPLIED  -- a pointer was moved onto --
  onmousemove %Script;       #IMPLIED  -- a pointer was moved within --
  onmouseout  %Script;       #IMPLIED  -- a pointer was moved away --
  onkeypress  %Script;       #IMPLIED  -- a key was pressed and released --
  onkeydown   %Script;       #IMPLIED  -- a key was pressed down --
  onkeyup     %Script;       #IMPLIED  -- a key was released --

Since it doesn't appear as though any of those will help you, you're probably best off to add the event to the onload of the body element (or through a more robust means, like jQuery's document ready function). Here's the quick hack way to do it:

<body onload="document.getElementById('post-comment').disabled=false">
nickf
That won't work...The asker wants the button disabled until JS is active, and onload requires JS enabled.
The Elite Gentleman
Elite Gentleman: You're not making any sense.
Eli Grey
+1  A: 

How about:

<script> 
function enableInput(){
    document.getElementById('post-content').disabled=false;
}
</script>
<body onload="enableInput();">
...
<input id="post-comment" type="submit" 
    value="Post Your Comment" disabled="true"/>

This will load the input button disabled by default and execute the javascript to enable it on load of the web page (which of course will only happen with javascript enabled.

Chris Knight
+1  A: 

Based on your sample code

<input id="post-comment" type="submit" value="Post Your Comment" disabled="disabled" />

then on javascript

<script type="text/javascript">
   window.onload = function() {
     document.getElementById('post-comment').disabled=false;
   }

   //OR using JQuery
   $(document).ready(function() {
      document.getElementById('post-comment').disabled=false;
   });
</script>

//That's it really.

Hope this helps.

The Elite Gentleman
+1  A: 

You want this:

<input id="post-comment" type="submit" value="Post Your Comment" disabled="disabled"/>
<script type="text/javascript">
document.getElementById("post-comment").disabled = false;
</script>

To everyone else: Why are you waiting for onload?

Eli Grey
A: 

By "unless javascript is on", do you mean that a form's submit should be disabled unless JS is supported on that browser?

If yes, you should make the submit button disabled by default. Write your JS code to enable the submit button in comment block (<!-- -->). Also use the <noscript> tag to inform the client that functionality is not available until JS is made enabled on the client browser.

Vishal Seth