Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On change disable a text box

Status
Not open for further replies.

amillia

Programmer
Nov 14, 2001
124
0
0
US
The javascipt I am working on that does not work goes like this:

<script language="javascript">
function assign_dte (){
if (document.form.test_one.option.value = "test1" ){
document.form.test_two.readonly="true"
}
}
</script>
 
Oops I posted that prematurely.

Any way, when someone chooses test1 in a selection drop down box then I want the second field which is test_two to be disabled or readonly. This isn't working yet. What do I need to do. Thank you for your help.
 
1) You should use the forms and elements collections when accessing form elements (or preferably getElementById instead).
2) You do not need to put .option when accessing the value of the dropdown. If you want to put in .option, you need to specify which option you are accessing (specifically in your case you would have to reference the selectedIndex value from this dropdown - it's the long way of doing things and completely unnecessary.)
3) You need to set the readOnly value to "readonly"
4) When referencing the readOnly value with javascript you have to capitalize the O in Only - javascript is case sensitive

So, here would be your amended code:
Code:
<script language="javascript">
function assign_dte (){
    if (document.forms["formName"].elements["test_one"].value = "test1" ){
        document.forms["formName"].elements["test_two"].readOnly="readonly"
    }
}
</script>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
try this:

Code:
<script language="javascript">
function assign_dte (){
    var e = document.forms['yourFormName'].elements;
    if (e['test_one'].options[e['test_one'].selectedIndex].value == "test1" ){
        e['test_two'].readonly="true";
    }
}
</script>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
ahhh.... one more thing:

5) when checking for equality using javascript you have to use a double equals (==) - single equals (=) sets values.

So, here's the final amended code:
Code:
<script language="javascript">
function assign_dte (){
    if (document.forms["formName"].elements["test_one"].value == "test1" ){
        document.forms["formName"].elements["test_two"].readOnly="readonly"
    }
}
</script>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
final result:

Code:
<script language="javascript">
function assign_dte (){
    var e = document.forms['yourFormName'].elements;
    if (e['test_one'].options[e['test_one'].selectedIndex].value == "test1" ){
        e['test_two'].readOnly= 'readonly';
        // you could also do this:
        // e['test_two'].disabled = true;
    }
}
</script>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I swear I caught that before you said something. Honestly [smile]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Works beautifully, you guys are awesome as usual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top