Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2009, 09:44 PM   PM User | #1
jasonc310771
Regular Coder

 
Join Date: Mar 2006
Posts: 478
Thanks: 3
Thanked 0 Times in 0 Posts
jasonc310771 can only hope to improve
js not working. on select disable other fields

i have found the following code and altered it to disable certain fields in my form.

but it does not swap the fields around from disabled to enabled and visa versa.

what am i doing wrong?

the following javascript code is in a file and i use the <link... > method within the <head> tags to include this script

Code:
function txtDisable( theForm ){
        cBox = theForm.NAME;

        if( cBox.options[cBox.selectedIndex].value == "st" )
		theForm.INP1.disabled = true;
		theForm.INP1.value = "";

		theForm.INP2.disabled = false;
		theForm.INP2.value = "";
        else
		theForm.INP1.disabled = false;
		theForm.INP1.value = "";

		theForm.INP2.disabled = true;
		theForm.INP2.value = "";
}


Code:
<form action="" method="post" enctype="multipart/form-data" name="edit">
<select name="NAME" onChange="txtDisable( this.form );">
<option value="SELECT AN OPTION">SELECT AN OPTION</option>
<option value="st">NOT READY YET</option>
</select>
<input name="INP1" type="text" size="50">
<input name="INP2" type="text" size="50" disabled>
</form>
jasonc310771 is offline   Reply With Quote
Old 03-15-2009, 10:15 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Main problem is missing braces {}. And although NAME is upper case, it is best to avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

Code:
<script type = "text/javascript">

function txtDisable( theForm ) {
cBox = theForm.selname.value;
if( cBox == "st" ) {
theForm.INP1.disabled = true;
theForm.INP1.value = "";
theForm.INP2.disabled = false;
theForm.INP2.value = "";
}

else {
theForm.INP1.disabled = false;
theForm.INP1.value = "";
theForm.INP2.disabled = true;
theForm.INP2.value = "";
}
}

</script>

<form name="edit" action="" method="post" enctype="multipart/form-data" >

<select name="selname" onChange="txtDisable( this.form );">
<option value="SELECT AN OPTION">SELECT AN OPTION</option>
<option value="st">NOT READY YET</option>
</select>

<input name="INP1" type="text" size="50">
<input name="INP2" type="text" size="50" disabled>
</form>

When trouble arises and things look bad, there is always one individual who perceives a solution and is willing to take command. Very often, that individual is crazy.
Philip M is offline   Reply With Quote
Old 03-15-2009, 11:26 PM   PM User | #3
jasonc310771
Regular Coder

 
Join Date: Mar 2006
Posts: 478
Thanks: 3
Thanked 0 Times in 0 Posts
jasonc310771 can only hope to improve
great that done the job.

but just one thing how do i get the page to show the options disabled that need to be on first load.
jasonc310771 is offline   Reply With Quote
Old 03-15-2009, 11:46 PM   PM User | #4
jasonc310771
Regular Coder

 
Join Date: Mar 2006
Posts: 478
Thanks: 3
Thanked 0 Times in 0 Posts
jasonc310771 can only hope to improve
just thought, also i need to make sure it does not clear the fields if someone has tried to enter something but did not fill out the form correctly as it will come back to the same page and place all the existing data back in to the fields that were filled out.


also i have thought about how the page would work and think i should be putting something at the end of the page as the form does not exist if i was to place the reset script at the top, but having said that i still need to make sure that the form is not reset if some of the fields were filled out.

this would be the parts that need resetting...

theForm.INP1.disabled = false;
theForm.INP1.value = "";
theForm.INP2.disabled = true;
theForm.INP2.value = "";

but how do i get it to do this on the form named 'myform'
jasonc310771 is offline   Reply With Quote
Old 03-16-2009, 12:20 AM   PM User | #5
jasonc310771
Regular Coder

 
Join Date: Mar 2006
Posts: 478
Thanks: 3
Thanked 0 Times in 0 Posts
jasonc310771 can only hope to improve
i thought this may work but does not seem to disable to fields that need to be disabled, field3, field4 and field5


Code:
<script type="text/javascript"><!--
if (document.edit.Submit.value!="SAVE CHANGES") {
document.edit.field1.disabled = false;
document.edit.field2.disabled = false;
document.edit.field3.disabled = true; theForm.field3.value = "";
document.edit.field4.disabled = true; theForm.field4.value = "";
document.edit.field5.disabled = true; theForm.field5.value = "";
}
--></script>
jasonc310771 is offline   Reply With Quote
Old 03-16-2009, 04:16 AM   PM User | #6
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
also i have thought about how the page would work and think i should be putting something at the end of the page as the form does not exist if i was to place the reset script at the top, but having said that i still need to make sure that the form is not reset if some of the fields were filled out.
Try changing this:
Code:
<form name="edit" action="" method="post" enctype="multipart/form-data"
 onsubmit="return false">
while you are testing the rest of the script
jmrker is offline   Reply With Quote
Old 03-16-2009, 08:33 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jasonc310771 View Post
i thought this may work but does not seem to disable to fields that need to be disabled, field3, field4 and field5


Code:
<script type="text/javascript"><!--
if (document.edit.Submit.value!="SAVE CHANGES") {
document.edit.field1.disabled = false;
document.edit.field2.disabled = false;
document.edit.field3.disabled = true; theForm.field3.value = "";
document.edit.field4.disabled = true; theForm.field4.value = "";
document.edit.field5.disabled = true; theForm.field5.value = "";
}
--></script>
Because you have disabled the field before you try to change its value, so as it is disabled nothing happens.

theForm.field3.value = ""; theForm.field3.disabled = true;
theForm.field4.value = ""; theForm.field4.disabled = true;
theForm.field5.value = ""; theForm.field5.disabled = true;
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.