codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:

Home » Javascript » Article

Javascript - Enable and Disable form elements

Article by:  Jeff Anderson  ( 1361 ) (3/6/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Viewed: 266241 times Rating (247 votes): 
 4.2 out of 5
 Rate this Article  Read Comments  Post Comments


This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.

Here is a standard text field:



Now here it is again, disabled:



Try typing in the second box, and you'll see that you can't - it's been disabled. You can do the same any other elements in a form - checkboxes, selection boxes and the rest.

Disabling Form Elements - Common Uses

One common use of this is to only enable the element, once the appropriate field has been selected earlier in the form.

Now here's a practical example. The 'explain your choice' field only becomes enabled when I have selected the answer to Question 1. Before selecting a radio button, try typing in the text field - you can't!

Question 1
Answer a:
Answer b:

Explain your choice:



Disabling Form Elements - How it's done

To disable an element, put the phrase disabled="true" within the element tag.



Once you've set the disabled tag, you can enable it using something like the following code:


 
<script language="javascript">

function enableField()
{
document.form1.address2.disabled=false;
}
 
</script>

<a href="javascript:enableField()">Click here to enable the element<a/>



With disabled set to false, the form element becomes useable.






CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on ' Javascript - Enable and Disable form elements'
Posted by :  Archive Import (Sujatha) at 08:46 on Friday, September 27, 2002
HI!!!

I am new to javascript and hence always browse the net trying to learn it. New doesn't mean I am starting from scratch. It just means i am developing with it right now only. Well I found this article useful. It was easy to understand and to the point .. not like some which cover the depths unnecessarily.

Good Work. :)
Posted by :  Archive Import (mike) at 22:45 on Tuesday, October 01, 2002
ok... i want to have the option on my site of selecting black and white or color with 2 checkboxes, which when you select one, it will bring up a pull down menu, which will have different sizes listed...
can you help me?
Posted by :  Archive Import (Ace) at 09:19 on Monday, October 28, 2002
I am using an image as a submit button:

<INPUT type="image" name="submitImage" src="something.jpg">

In the onsubmit event for the form(which passes the form), I have this:

frm.submitImage.disabled=true;

I am still able to click on the image and submit the form over and over, however. Is there something special I need to do to disable this type of form element?

I would like to prevent people from clicking the submit button (Image) more than once.

Thanks,
Ace
Posted by :  Archive Import (Dave) at 03:11 on Tuesday, October 29, 2002
Ace - does the same code work with a standard submit button - maybe you can't disable an image button - I've never tried it. Also you could try putting it in the onclick event of the button and see if that does anything, or even set a global variable which gets changed the first time you click and if its changed the form doesn't submit - I'm sure it should be possible the way you're doing it though.
Let us know if you crack it!
Posted by :  Archive Import (Wiz) at 20:20 on Tuesday, November 05, 2002
Use document.form.button.disable=true;

example:
frm.submitImage.submitImage.disabled=true;

-Enjoy
Posted by :  Archive Import ( ) at 05:36 on Wednesday, November 13, 2002
Posted by :  Archive Import (clintonG) at 12:24 on Thursday, November 14, 2002
This does not appear to function for me using IE6
Posted by :  Archive Import (vellay) at 21:01 on Sunday, November 24, 2002
your code will never work in nn4.7
Posted by :  Archive Import (cicagakid) at 23:58 on Wednesday, December 04, 2002
who cares about netscape?

the example above with the radio buttons and text field did not work for me.

WIN98 - IE6
Posted by :  Archive Import (Bill) at 17:04 on Wednesday, December 11, 2002
I am trying to use this script in a asp page and am not having much luck in making it work. Any suggestions?
Posted by :  Archive Import (Tez_Guy) at 02:06 on Monday, December 16, 2002
Hi all!!
How to make the disabling scripts work for both netscape and IE? Are'nt there any standard functions one can use for disabling form elements.
Another query is how to disable the file type input box alongwith the browse button??
Any suggestions are welcome...
Posted by :  Archive Import (pete) at 09:17 on Thursday, January 02, 2003
I got a javascript error from your example ;-)
Posted by :  Archive Import (matt) at 11:41 on Monday, January 06, 2003
thanks it works. Did one search and found this. IE6 something. Don't know if works in NS, but its for work, and we only use IE.
Posted by :  Archive Import (Neil Chuckervertty) at 06:39 on Thursday, January 09, 2003
Check your own site lots of javascript errors, ill correct it i work for freelancing.
bye
Posted by :  Archive Import (Sameer) at 06:51 on Wednesday, January 15, 2003
I am using an image as a button

<a href="javascript:trySave()" ><img name="save" border="0" src="save.gif" ></a>

In trySave() java script I have written
document.frm.save.disabled=true;

I am still able to click on the image second time.I want to disable it after clicking once
Posted by :  Archive Import (Afterdark) at 18:32 on Wednesday, January 15, 2003
Nice peice of code, simple, to the point & functional. Many Thanks
Posted by :  Archive Import (Erika) at 09:24 on Wednesday, January 29, 2003
Is there a way to have the appearance of the text box change so that it looks disabled?
Posted by :  Archive Import (Wael Shubeir) at 05:20 on Tuesday, February 04, 2003
Is there a way to create new form elements (e.g. input textbox) and display it according to user action.

Example:
When the user enters article number, a new textbox will be displayed.

Thanks
Posted by :  Archive Import (Surya) at 15:35 on Saturday, February 08, 2003
How do i disable a button if the submission date of a form equals to the current date?
Thanks!
Posted by :  Archive Import (anonymous) at 13:50 on Thursday, March 06, 2003
For those of you who are having trouble disabling <input type="image" submit button problems.. I was able to make it work.

I just called onclick="var obj =disable(this);" for the submit button. I then had an onSubmit event for my form and passed in obj.

ie onSubmit="return disablebutton(obj);"
function disablebutton(obj){
obj.disabled=true;
}

I hope this helps..
Posted by :  Archive Import (Nicu) at 13:11 on Thursday, March 13, 2003
I need a java script that does the following: i have a banner and a disabled button on a page. the button will become enabled only when someone clicks the banner first. if someone cand help me pls mail me at perf_uzzi@cogeco.ca 10x
Posted by :  Archive Import (amit singh) at 01:14 on Friday, April 25, 2003
in one html form , we use many button,list box.when user select one buttun then all buttun in form should disable.we this do in javascript.
Posted by :  Archive Import (Guy Derriman) at 05:13 on Wednesday, April 30, 2003
This worked for me in IE 6

if (document.getElementById("_ctl0_pPrimaryFromCalendar_pDateImgBtn") != null)
{
var pPrimaryFromCalendarDateImgBtn = document.getElementById("_ctl0_pPrimaryFromCalendar_pDateImgBtn");
pPrimaryFromCalendarDateImgBtn.disabled = true;
}
Posted by :  Archive Import (Roks James from Win infy tech NY) at 09:44 on Wednesday, April 30, 2003
We have 2 text field. and 2 radio buttons.
if we select one radio button the one text field should avtivate and rest of the text fields should deactivate (greyed).
Kindly suggest the code or script if any java script guru is available on the net.
Posted by :  Archive Import (harry foxluv) at 11:20 on Thursday, May 01, 2003
Actually I have a question of my own on 'How To Disable a Function' using Java Script.

I know how to use some Java Script. I can disable the "Right Click Mouse Function" and Put a "BOX" with text.

What I want to do on my Website is disable the webpage "COPY" function in IE 6.0 0r above ...
File
Save As

- and -
If possible, I would also like to disable the "PRINT" function in IE as well.
File
Print

I have searched many places but have not been able to find anyone who knows how to disable these functions.

Disabling the entire IE File Menu would be acceptable, but not preferable.

Can you assist me?
I am pretty new at this overall.
If you could give me the Java Script or HTML Code to insert that would be best.
Thanks,
Harry
Posted by :  Archive Import (siva) at 04:32 on Thursday, May 29, 2003
PLZ HELP ME -- URGENT

THANKS IN ADVANCE

I HAVE SOME BUTTONS IN MY SCREEN.

I HAVE TO DISABLE SOME BUTTONS IF THE CONDITION IS TRUE ELSE THE OTHER BUTTONS TO BE DISABLED.

I HAVE GIVEN THESE BUTTONS IN THE <FILEDSET> TAG.

MY FORM NAME IS MYFORM -- THE CODE IS
DOCUMENT.MYFORM.REM_DELETE.DISABLED=TRUE

IT'S THROWING AN ERROR AS 'REM_DELETE' IS NOT AN OBJECT.

WHY IT IS SO????
Posted by :  Archive Import (xrellix) at 16:34 on Friday, May 30, 2003
If u have a submit input of type image (rather than a button) then use the following
method to disable the image and submit the form:

<input name="Search" type="image" src="/resources/images/buttons/search_bt_bl.gif" onclick="doSubmit(this)"></td>

function doSubmit(submitTypImage) {
submitTypImage.disabled = true;
submitTypImage.form.submit();
}

When u have a submit button (rather than an image) u don't need to sumbit() the form explicitly,
just the disable will do. In the case of a button the form gets submitted implicitly by the browser
when its clicked and so u can just have onclick="this.disabled=true" right on the <input> tag.
This is however not true for image type input submits.
Posted by :  Archive Import (Ali) at 11:23 on Wednesday, June 04, 2003
Still I can't disable the image submit button :(

I have the following example:

<SCRIPT language=JavaScript> function Validate(FormName) {
if (FormName.FieldName.value == "") {
alert("Field is Null");
return (false);
}
FormName.SubmitImage.disabled=true;
return (true);
} </script>
<form name="FormName" action="test.html" onSubmit="return Validate(this)">
<input name="FieldName"><input type="Image" src="Image.gif" name="SubmitImage">
</form>
Posted by :  Archive Import (Olivier) at 08:34 on Monday, June 16, 2003
disable and image type buttons...

an easy way to do it :
use a function to check your different fields...

<FORM NAME="the_form" ACTION="..." METHOD="POST" onsubmit="return check_form()">

<script language="JavaScript">
var submitted=false;
function check_form() {
if (submitted == true) {
alert ("form already submitted") ;
return false;
}
else {
// everything is ok check the fields, ie using if (document.... .lenght == 0) alert...
submitted = true ;
}
}
</script>

It disables the aiblity to validate twice a form...
simple and efficient !
Posted by :  Archive Import (susmita) at 12:41 on Friday, June 20, 2003
I need a Javascript which disables the form fields only after submitting the data to a database.I am working with PHP and MS Access 2000 on an ONLINE APPLICATION.
Thanks!!
Posted by :  Archive Import (Neo) at 10:24 on Wednesday, June 25, 2003
If you want to make a script work only with IE, you are a suck webmaster, and even a stupid computer user!!
Posted by :  Archive Import (kusalo) at 02:41 on Saturday, July 05, 2003
I got it to work - excellent. I can see a variety of uses for this (tho yet to test it in NS :)

But...
to get it working I had to read the page source and extract the <form name=form22>until</form> bit before I really understood.
It would be useful to have this presented as the code snippet.

many thanks
Posted by :  Archive Import (hussain) at 07:32 on Wednesday, July 16, 2003
I need code to disable a radio button by selecting a particular value in a Drop Down Menus
Posted by :  Archive Import (test) at 08:44 on Sunday, August 03, 2003
does not work under IE 5.5
Posted by :  Archive Import (test) at 08:45 on Sunday, August 03, 2003
Would be nice to disable the 'Submit Comment' button in this page no ? ;o)
Posted by :  Archive Import (Kapil Kumar Varshney) at 01:07 on Friday, August 22, 2003
Hi,
Can anyone guide me to disable all the elements (show the fields on the page as readonly) by javascript?
I want to make the page as readonly
Posted by :  Archive Import (M1K3 R0CK3T) at 11:10 on Friday, August 22, 2003
OK, I'm using this script in its simplest form. I have 2 checkboxes. The second is disabled until you click the first one.
That works fine.
But if you unclick the first checkbox, the second is still enabled.

Please Help!
Posted by :  Archive Import (jumbo) at 23:33 on Monday, September 08, 2003
no go in IE 6 .. tried many times
Posted by :  grace at 13:43 on Tuesday, February 03, 2004
Just want to ask how to enable and disable textbox if the record is dynamically generated.
The text box will become enable depending on the checked checkbox which is also dynamic.
Hope i can find the right code this time.
thanks in advance!
Posted by :  tdesai at 12:29 on Friday, August 13, 2004
I have a password field defined like

<input type="password" name="oldpassword" size="16">
Based on certain conditions, I need to enable it and
disable it

So I have a Javascript function wherein I am
coding the same as below

function some Func(..)
{
.....
if (condition) {
document.formname.oldpassword.disabled=true;
} else {
document.formname.oldpassword.disabled=false;
}
....
}

In Mozilla, it works the way I expected
meaning - the background color of the textfield
changes drastically enough to give the user the
impression that the field has been disabled

In IE (both 5.x and 6.0), the disabled doesn't allow
me to edit the password field but it does not dim/gray out
the password field to give the visual cue as to the fact
that this field has been disabled.

Any help/pointers would be very much appreciated..

thx
Tushar
Posted by :  Nusem at 07:49 on Monday, October 04, 2004
Notice That the code is writtem in JScript and that True and False are case sensitive,
in case you get run time error:
"variable 'True' is undefined"
Posted by :  Cyber at 01:15 on Wednesday, October 06, 2004
i wnat to know abt how to disable the submit button when the text box is empty can anyone send me the script i am new to this field

Posted by :  vishu_k26 at 03:48 on Tuesday, October 19, 2004
Hi,
Can anyone guide me to disable all the elements (show the fields on the page as readonly) by javascript?
I want to make the page as readonly

Urgent
Posted by :  casperjez at 23:36 on Thursday, October 21, 2004
Hi,

Does anyone have some code for changing the disabled fields text color? I want to change it from the greyed out color to something different. Can this even be done?

casp
Posted by :  aleksm at 00:36 on Monday, December 06, 2004
I am also playing with image buttons. But instead I want to enable the button until there is a change in one of you text elements.

So, this is what I have so far:

This should enable the image button

<script language="javascript">
function enablebutton(submitTypImage) {
submitTypImage.disabled = true;
}
</script>


The button simply has disabled="true" within the element tag

On my text element I have:

onChange=="enablebutton(this)"

I GET THIS ERROR: "UPDATEBUTTON IS UNDEFINED"

"UPDATEBUTTON" is the id and name of the image button.

What am I missing ?
Posted by :  vwebdev at 11:12 on Friday, March 11, 2005
Hello,

I found this article very helpfull and concise,
yet I have a forward question about accessing elements width "document" and "elements"
combination. I use aspx development platform supported by microsoft, does anyone use this platform and have accessed "hidden" elements ex. dropdown list whose visible property is set to false. I have tried to use document.getElementById("ElementName") methods but have failed to get reach of them. As far as I know the problem is that hidden elements are not accessible by javascript script which is located and executed at client side. Could anyone suggest how can I get values and index of selected element in hidden drop down list.

Any help is appreciated.

Regards

Simon
Posted by :  Intsik at 11:48 on Saturday, April 02, 2005
i have read this article and it is excellent. but with regards to this topic, i would like to seek solution to this problem.

i have two combo boxes in my form. the enabling and disabling of the second combobox depends on the value of the first combobox.
for example, i only want to enable my second combobox if the value of the first combobox is A or B..
Posted by :  sai_cool at 21:59 on Friday, April 15, 2005
i want to know how do i disable a 'back' button from the page which is present on the browser.plz can someone tell me the sol.
Posted by :  asif at 21:53 on Friday, April 29, 2005
I've put this line for disabling select box for validatin of form but it doesn't work, is there any body to helf me out

document.form1.selectbox.disabled = true;

cheers..
Posted by :  roders at 06:01 on Wednesday, June 29, 2005
Hi concerning the enabling field in form elements.
I would like to know if it exists 1 that would disabled certain field when the user select an option from a drop down list?

Thanks.
Posted by :  d32207 at 11:55 on Monday, September 19, 2005
Hi,
I would like to display a text box depending on if a checkbox was checked or not. When the checkbox is checked, I want the text box to appear. When it is unchecked, I want the text box to disappear.
Thanks in advance!
Posted by :  goldguy007 at 16:50 on Friday, September 23, 2005
i am using something similar to the code below to disable an image submit .
however, instead of going back to the page in process after hitting ok on the alert window (this is after the button has been hit a second time) it goes to a browser window with the word 'false' in it (from the return false; statement).

i just want the initial submission to continue. any clue why this is?????
thanks for the help

<script language="JavaScript">
var submitted=false;
function check_form() {
if (submitted == true) {
alert ("form already submitted") ;
[b]return false;[/b]
}
else {
submitted = true ;
[b]document.form.submit();[/b]
}
}
Posted by :  raj_fandoo at 00:31 on Monday, October 17, 2005
i have a form on which i am opening one popup window so that user can select data which are coming from database.

and after selection user is supposed to click on OK and i am refreshing the page with the help of window.opener(). now my problem is that the on click of OK on popup the page load of Parent page(base page) is getting called twice.


can some body please help me out on this. thanks in advance.



To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
Javascript Onload Event
by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Javascript - Enable and Disable form elements
by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Form Validation Function
by Jeff Anderson
A javascript validation function that you can use to validate all types of forms.
Check IsNumeric Function
by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric.
JavaScript Field Is Empty Form Validation
by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not.
Check Email Validation Function
by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form.
Multiple submit buttons on a single form
by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button.
Validate Form and Disable Submit Button
by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating.
Simple date validation
by Chris Hogben
function that takes a date in three parts, day, month and year - returns true if it's a valid date.
Multiple submit buttons with form validation
by Paul Eckert
This code shows how to redirect a user to multiple sites, depending on which submit button he presses, but only after the form validates correctly.








Recent Forum Threads
• Access https in http page
• IE page Redirect
• Re: Javascript problem with document.write and accented characters
• Re: sorting and Linked list
• Re: need help linked list
• Re: Help with arrays
• Re: Reading from a file
• Re: Why Use Method?
• Re: Help with a simple program


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005