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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enable or disable textbox 1

Status
Not open for further replies.

smurf01

IS-IT--Management
Jul 6, 2002
470
GB
Guys,
I am very new to javascript and what I am trying to do is make a textbox enabled or disabled depending on the value in another textbox. below is the code i am trying out, i know it does not work and would appreciate any help. the two textbox names are

txtPriceOut
txtPriceInc

Code:
<script>
function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;PI&quot;)
boxDis=(&quot;txtPriceOut&quot;)
 if(boxDis==0)
 {
 PI.style.visibility = &quot;enabled&quot;;
 	PI.style.display =&quot;block&quot;;
}
PI.style.visibility = &quot;disabled&quot;;
{
}
</script>

Regards

Paul
 
function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;PI&quot;)
if(txtPriceOut.value==0){
txtPriceOut.disabled = true;
}else{
txtPriceOut.disabled = false;
}
 
Simon,
Thanks for your speedy response, I'm sorry but I don't think i explained myself very well and I also think that my code did not help either, I will try again

I have two textboxes

txtPriceOut & txtPriceInc

what I would like is if the value of txtPriceOut is = 0 then txtPriceInc is &quot;enabled&quot;

otherwise txtPriceInc is &quot;disabled&quot;

Regards

Paul
 
function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)

if(txtPriceOut.value==0){
txtPriceInc.disabled = true;
}else{
txtPriceInc.disabled = false;
}
 
Simon,
Unfortunatley it is still not working when i open the page even if the value of txtPriceOut is > 0 the txtPriceInc box is still enabled. I have posted my code if you don't mind looking at it

<%@LANGUAGE=&quot;VBSCRIPT&quot; CODEPAGE=&quot;1252&quot;%>
<!--#include file=&quot;../Connections/Estimate.asp&quot; -->
<%
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables(&quot;SCRIPT_NAME&quot;))
If (Request.QueryString <> &quot;&quot;) Then
MM_editAction = MM_editAction & &quot;?&quot; & Request.QueryString
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = &quot;&quot;
%>
<%
' *** Update Record: set variables

If (CStr(Request(&quot;MM_update&quot;)) = &quot;frmUpdate&quot; And CStr(Request(&quot;MM_recordId&quot;)) <> &quot;&quot;) Then

MM_editConnection = MM_Estimate_STRING
MM_editTable = &quot;tblEstData&quot;
MM_editColumn = &quot;ID&quot;
MM_recordId = &quot;&quot; + Request.Form(&quot;MM_recordId&quot;) + &quot;&quot;
MM_editRedirectUrl = &quot;EFACMSearch.asp&quot;
MM_fieldsStr = &quot;txtDateSup|value|txtOrder|value|txtPriceOut|value|txtPriceInc|value|cmbComments|value|txtFeed|value|chkCompleted|value&quot;
MM_columnsStr = &quot;Date_Supplied|',none,NULL|Order_Placed|',none,''|Price_Out_Percent|none,none,NULL|Increase_Price|none,none,NULL|Comments|',none,''|Feedback|',none,''|Completed|none,'Y','N'&quot;

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, &quot;|&quot;)
MM_columns = Split(MM_columnsStr, &quot;|&quot;)

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> &quot;&quot; And Request.QueryString <> &quot;&quot;) Then
If (InStr(1, MM_editRedirectUrl, &quot;?&quot;, vbTextCompare) = 0 And Request.QueryString <> &quot;&quot;) Then
MM_editRedirectUrl = MM_editRedirectUrl & &quot;?&quot; & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & &quot;&&quot; & Request.QueryString
End If
End If

End If
%>
<%
' *** Update Record: construct a sql update statement and execute it

If (CStr(Request(&quot;MM_update&quot;)) <> &quot;&quot; And CStr(Request(&quot;MM_recordId&quot;)) <> &quot;&quot;) Then

' create the sql update statement
MM_editQuery = &quot;update &quot; & MM_editTable & &quot; set &quot;
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),&quot;,&quot;)
MM_delim = MM_typeArray(0)
If (MM_delim = &quot;none&quot;) Then MM_delim = &quot;&quot;
MM_altVal = MM_typeArray(1)
If (MM_altVal = &quot;none&quot;) Then MM_altVal = &quot;&quot;
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = &quot;none&quot;) Then MM_emptyVal = &quot;&quot;
If (MM_formVal = &quot;&quot;) Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> &quot;&quot;) Then
MM_formVal = MM_altVal
ElseIf (MM_delim = &quot;'&quot;) Then ' escape quotes
MM_formVal = &quot;'&quot; & Replace(MM_formVal,&quot;'&quot;,&quot;''&quot;) & &quot;'&quot;
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_editQuery = MM_editQuery & &quot;,&quot;
End If
MM_editQuery = MM_editQuery & MM_columns(MM_i) & &quot; = &quot; & MM_formVal
Next
MM_editQuery = MM_editQuery & &quot; where &quot; & MM_editColumn & &quot; = &quot; & MM_recordId

If (Not MM_abortEdit) Then
' execute the update
Set MM_editCmd = Server.CreateObject(&quot;ADODB.Command&quot;)
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl <> &quot;&quot;) Then
Response.Redirect(MM_editRedirectUrl)
End If
End If

End If
%>
<%
Dim RSUpdate__MMColParam
RSUpdate__MMColParam = &quot;1&quot;
If (Request.QueryString(&quot;ID&quot;) <> &quot;&quot;) Then
RSUpdate__MMColParam = Request.QueryString(&quot;ID&quot;)
End If
%>
<%
Dim RSUpdate
Dim RSUpdate_numRows

Set RSUpdate = Server.CreateObject(&quot;ADODB.Recordset&quot;)
RSUpdate.ActiveConnection = MM_Estimate_STRING
RSUpdate.Source = &quot;SELECT * FROM tblEstData WHERE ID = &quot; + Replace(RSUpdate__MMColParam, &quot;'&quot;, &quot;''&quot;) + &quot;&quot;
RSUpdate.CursorType = 0
RSUpdate.CursorLocation = 2
RSUpdate.LockType = 1
RSUpdate.Open()

RSUpdate_numRows = 0
intPercent=RSUpdate.Fields.Item(&quot;Price_Out_Percent&quot;).Value/100
%>
<html>
<head>
<title>Estimating Feedback</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script>
function checkBoxes(){
boxArr = new Array(&quot;txtDateSup&quot;, &quot;txtOrder&quot;, &quot;cmbComments&quot;)
dataFound = &quot;No&quot;
for (x=0; x<boxArr.length; x++){
boxVal = eval (&quot;document.frmUpdate.&quot; + boxArr[x] + &quot;.value&quot;)
if (boxVal != &quot;&quot;){
dataFound = &quot;Yes&quot;
}
}
document.frmUpdate.txtFeed.value = dataFound
}
</script>

<script>
function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)

if(txtPriceOut.value==0){
txtPriceInc.disabled = true;
}else{
txtPriceInc.disabled = false;
}
</script>

<link href=&quot;EFStyles.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
<script language=&quot;JavaScript&quot; src=&quot;../CleaningSchedules/date-picker.js&quot;></script>
</head>

<body>
<div align=&quot;center&quot;>    Clicking on the Smurfit Logo will take you back
to the Home Page </div>
<form action=&quot;<%=MM_editAction%>&quot; method=&quot;POST&quot; name=&quot;frmUpdate&quot; id=&quot;frmUpdate&quot; onSubmit=&quot;checkBoxes()&quot;>
<table width=&quot;88%&quot; align=&quot;center&quot; style = &quot;border-bottom: 1px solid blue; border-left: 1px solid blue; border-top: 1px solid blue; border-right: 1px solid blue&quot;>
<tr bgcolor=&quot;#000099&quot;>
<td height=&quot;25&quot; colspan=&quot;4&quot;> <div align=&quot;center&quot;><strong><font color=&quot;#FFFFFF&quot;>ACCOUNT
MANAGER UPDATE FORM</font></strong></div></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td width=&quot;19%&quot; height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Record
ID : </div></td>
<td width=&quot;24%&quot; bgcolor=&quot;#0099FF&quot;> <div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;ID&quot;).Value)%></font></strong></div></td>
<td width=&quot;23%&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Date Supplied : </div></td>
<td width=&quot;34%&quot; bgcolor=&quot;#CCCCCC&quot;>    <input name=&quot;txtDateSup&quot; type=&quot;text&quot; id=&quot;txtDateSup3&quot; value=&quot;<%=(RSUpdate.Fields.Item(&quot;Date_Supplied&quot;).Value)%>&quot; size=&quot;12&quot; maxlength=&quot;10&quot;>
<a href=&quot;javascript:show_calendar('frmUpdate.txtDateSup');&quot; onMouseOver=&quot;window.status='Date Picker';return true;&quot; onMouseOut=&quot;window.status='';return true;&quot;><img src=&quot;../mycal.gif&quot; width=14 height=16 border=1 align=top></a>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Username :  </div></td>
<td><div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;UserName&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Order Placed : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>  
<input name=&quot;txtOrder&quot; type=&quot;text&quot; id=&quot;txtOrder3&quot; value=&quot;<%=(RSUpdate.Fields.Item(&quot;Order_Placed&quot;).Value)%>&quot; size=&quot;4&quot; maxlength=&quot;1&quot; ></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Spec No. :  </div></td>
<td><div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;SpecNo&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Price Out % : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>   <input name=&quot;txtPriceOut&quot; type=&quot;percent&quot; id=&quot;txtPriceOut&quot; value=&quot;<%= FormatNumber((RSUpdate.Fields.Item(&quot;Price_Out_Percent&quot;).Value), 2, -2, -2, -2) %>&quot; size=&quot;10&quot; maxlength=&quot;10&quot; ></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Customer :  </div></td>
<td><div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;Customer&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Price Increased : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>   <input name=&quot;txtPriceInc&quot; type=&quot;text&quot; id=&quot;txtPriceInc&quot; value=&quot;<%=((RSUpdate.Fields.Item(&quot;Increase_Price&quot;).Value))%>&quot; size=&quot;10&quot; maxlength=&quot;10&quot; ></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Account Manager : </div></td>
<td><div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;AccountM&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Present Comments : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>   <%=(RSUpdate.Fields.Item(&quot;Comments&quot;).Value)%></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Date In :  </div></td>
<td bgcolor=&quot;#0099FF&quot;>
<div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;Date_In&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Comments : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>   
<select name=&quot;cmbComments&quot; id=&quot;select&quot; title=&quot;<%=(RSUpdate.Fields.Item(&quot;Comments&quot;).Value)%>&quot; >
<option> </option>
<option>Unsuccesful - delay in Pricing</option>
<option>Unsuccesful - Due to Lead Time</option>
<option>Unsuccesful - Origination</option>
<option>Unsuccesful - Minimum Run to Long</option>
<option>Unsuccesful - Withdrawn Line</option>
<option>Unsuccesful - Design Alteration</option>
</select></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Date Out : </div></td>
<td><div align=&quot;left&quot;><strong><font color=&quot;#FFFFFF&quot;> <%=(RSUpdate.Fields.Item(&quot;Date_Out&quot;).Value)%></font></strong></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Feedback : </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>   
<input name=&quot;txtFeed&quot; type=&quot;text&quot; id=&quot;txtFeed2&quot; value=&quot;<%=(RSUpdate.Fields.Item(&quot;Feedback&quot;).Value)%>&quot; size=&quot;4&quot; maxlength=&quot;3&quot;></td>
</tr>
<tr bgcolor=&quot;#0099FF&quot;>
<td height=&quot;25&quot; bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;></div></td>
<td><div align=&quot;left&quot;></div></td>
<td bgcolor=&quot;#FFFFFF&quot;> <div align=&quot;right&quot;>Completed: </div></td>
<td bgcolor=&quot;#CCCCCC&quot;>  
<input name=&quot;chkCompleted&quot; type=&quot;checkbox&quot; id=&quot;chkCompleted&quot; value=&quot;checkbox&quot;></td>
</tr>
<tr bgcolor=&quot;#000099&quot;>
<td height=&quot;25&quot; colspan=&quot;4&quot;>
<div align=&quot;center&quot;>
<input name=&quot;Update&quot; type=&quot;submit&quot; id=&quot;Update&quot; value=&quot;Update&quot;>
</div></td>
</tr>
</table>
<input type=&quot;hidden&quot; name=&quot;MM_update&quot; value=&quot;frmUpdate&quot;>
<input type=&quot;hidden&quot; name=&quot;MM_recordId&quot; value=&quot;<%= RSUpdate.Fields.Item(&quot;ID&quot;).Value %>&quot;>
</form>
</body>
</html>
<%
RSUpdate.Close()
Set RSUpdate = Nothing
%>



Regards

Paul
 


A function will only work when its called - I thought you had taken care of that.

place this in the head between the script tags:
function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;str1&quot;)
var txtPriceInc=document.getElementById(&quot;str2&quot;)

if(txtPriceOut.value==0)
txtPriceInc.disabled = true;
}


change the body tag to this:
<body onLoad=&quot;ToggleTextBoxDisplay()&quot;>

 
My fault - it's been a long day

function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)
if(txtPriceOut.value==0)
txtPriceInc.disabled = true;
}
 
Simon, Thanks for all your help unfortunately it still does not work, never mind I'll keep trying

:-D [2thumbsup] :-D

Regards

Paul
 
This should work.

what values are alerted ?

function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)
alert(txtPriceOut)
if(txtPriceOut.value=='0')
txtPriceInc.disabled = true;
alert(txtPriceInc.disabled)
}
 
Simon,
the first alert displays [object]
the second alert displays [false]

Does this help ??

Regards

Paul
 

the alert should show the value of the text box - i have converted it to an integer - if the value of the text box is 0 then txtPriceInc will be disabled - I have added another alert

function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)
alert(txtPriceOut.value)
if(parseInt(txtPriceOut.value)<1){
txtPriceInc.disabled = true;
alert(&quot;txtPriceInc is now disabled&quot;)
}
}


 
Simon,
I changed the following :
Code:
if(parseInt(txtPriceOut.value)<1){

to

Code:
if(parseInt(txtPriceOut.value)>0){

because I only want txtpriceInc disabled if their was a price set against it.

Anyway all is working fine now so I am going to remove the alerts !!

Thanks for your help and perseverance, you deserve a &quot;STAR&quot;.

:-D [2thumbsup] :-D



Regards

Paul
 
Simon,
I hope you don't mind but another question for you.
At the moment when the page opens the code works fine however if the user changes the value of txtPriceOut to 0 is there a way to enable txtPriceInc again dynamically. Otherwise the only option is to update the form and then refresh in order to be able to use txtPriceInc


Regards

Paul
 


change this line

<input name=&quot;txtPriceOut&quot; type=&quot;percent&quot; id=&quot;txtPriceOut&quot; value=&quot;<%= FormatNumber((RSUpdate.Fields.Item(&quot;Price_Out_Percent&quot;).Value), 2, -2, -2, -2) %>&quot; size=&quot;10&quot; maxlength=&quot;10&quot; >

to

<input onChange=&quot;ToggleTextBoxDisplay()&quot; name=&quot;txtPriceOut&quot; type=&quot;percent&quot; id=&quot;txtPriceOut&quot; value=&quot;<%= FormatNumber((RSUpdate.Fields.Item(&quot;Price_Out_Percent&quot;).Value), 2, -2, -2, -2) %>&quot; size=&quot;10&quot; maxlength=&quot;10&quot; >

and the function will run each time the input box is changed

you will need to change the function to


function ToggleTextBoxDisplay(){
var txtPriceOut=document.getElementById(&quot;txtPriceOut&quot;)
var txtPriceInc=document.getElementById(&quot;txtPriceInc&quot;)
if(parseInt(txtPriceOut.value)>0){
txtPriceInc.disabled = true;
}else{
txtPriceInc.disabled = false;
}
}


 
Simon,
I tried out your amended code and it works really well, thanks for your help.

:-D [2thumbsup] :-D

Regards

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top