/    Sign up×
Community /Pin to ProfileBookmark

assigning values to form elements from changes in other form elements

I’m trying to clear a text box input from a form as a result of selecting an item from a select box within the same form. I have been able to do this using the onchange event handler. However, in this case the names of the form elements are created on the fly due to not knowing how many records from a database will come up. The names of the forms are created while looping through the records in the database. for example the Name (of the select element) is: (i is the loop control variable)

NAME=”<%= “Visitor2(” & i & “)” %>”

the name of the text input is: NAME=”<%= “Visitor(” & i & “)” %>”

I want Visitor to set the Visitor2(i) element to 0 when the onchange event occurs for the text field (Visitor). I want the text field Visitor to clear when someone selects an item for Visitor2. It works great if the names are not dynamic, but it will not work if the names are built on the fly through the use of a for loop.

Here’s the first part of the Select tag:
<SELECT class=tbFlat NAME=”<%= “Visitor2(” & i & “)” %>” onchange=”form1.Visitor(i).value=’ ‘”SIZE=1>

I’ve also tried changing the onchange to:
onchange=”<%= “form1.Visitor(” & i & “).value=’ ‘”

but that doesn’t work either..

any suggestions?

thanks,

kb

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@russellNov 15.2004 — Change this
<i>
</i>onchange="&lt;%= "form1.Visitor(" &amp; i &amp; ").value=' '"

to
<i>
</i>onchange="form1.Visitor(&lt;%=i%&gt;).value=' '"

And this
<i>
</i>NAME="&lt;%= "Visitor(" &amp; i &amp; ")" %&gt;"

to
<i>
</i>NAME="Visitor(&lt;%=i%&gt;)"

While you're at it, change the name of the select element from
<i>
</i>NAME="&lt;%= "Visitor2(" &amp; i &amp; ")" %&gt;"

To
<i>
</i>NAME="Visitor2(&lt;%i%&gt;)"
Copy linkTweet thisAlerts:
@senshiNov 15.2004 — <% & %> is reserved for VBscript.

VBscript and Javascript dont talk to each other they run independently, whilst it is not a problem running two scripts from different environments on the same page, you get issues when trying to set data by one to be read by another....

IF you MUST, use something called a safe array where VBscript can dump into an array its variables contents to be read by javascript.

Best all round soloution thouugh is to use one and not both so make your choice and either stick to javascript or VBscript to resolve your soloution.
Copy linkTweet thisAlerts:
@russellNov 15.2004 — 
<% & %> is reserved for VBscript

...

Best all round soloution thouugh is to use one and not both so make your choice and either stick to javascript or VBscript to resolve your soloution.
[/quote]

This is not quite correct. The <% %> tags delimit server-side script (could be JS or VBS), not client side VbScript. I'll agree that mixing client side scripting languages is a poor choice (in fact, I'd refrain from using client-side VbScript at all). But mixing server side vbScript with client-side JavaScript is definitely the right solution to many problems, including the one presented here. You should be sure that your forms still work without the JavaScript however if this is a WWW application. Be sure to perform server-side validation in addition to your client side validation.
Copy linkTweet thisAlerts:
@kball1958authorNov 15.2004 — I just tried your suggestions with "form1.Visitor<%=i%>.value=' '", etc. and now I get an error message that says:

A runtime error has occurred, do you wish to debug?

Error: Object doesn't support this property or method.

I don't know why it is gettting this error since it works with the same code (other than the fact that the names are statically named, rather than dynamic using the loop control variable.)

thanks for your help,

KB
Copy linkTweet thisAlerts:
@russellNov 15.2004 — post the html source of the document. you are getting a javascript error. need to see the code to know why.
Copy linkTweet thisAlerts:
@kball1958authorNov 18.2004 — Hi Russell,

thanks for the reply... the code associated with the form is:

<FORM METHOD="post" Name=form1 ACTION="EditTeamScheduleExecute.asp">

<input type="hidden" name=Sport value="<% = Session("MySport") %>" />
<input type="hidden" name="HomeTeam" value="form1.Team.value " />
<input type=hidden name=numrecs value=<%= numrecs %> />





<%

if (numrecs > 0) then

redim game(numrecs), gamedate(numrecs), loc(numrecs), scrim(numrecs), gametime(numrecs), visitorname(numrecs)



' Do While Not rs.EOF

For i = 0 to numrecs-1

game(i) = rs.Fields("GameID").Value
gamedate(i) = rs.Fields("Date").Value
loc(i) = rs.Fields("Location").Value
scrim(i) = rs.Fields("Scrimmage").Value
gametime(i) = rs.Fields("Time").Value
visitorname(i) = rs.Fields("VisitorTeamName").Value




%>

<tr>

<input type=hidden name="<%= "GameID(" & i & ")" %>" value="<%= game(i) %>" />


<td>
<input align=left type="checkbox" name="<%= "Delete(" & i & ")" %>" />
</td>
<td>
<input class=tbFlat name="<%= "Date(" & i & ")" %>" SIZE=20 MAXLENGTH=11 value ="<%= gamedate(i) %>" />

</td>
<td>

<%
set rstimes=server.CreateObject("ADODB.Recordset")
SQL_query = "SELECT * FROM GameTimes WHERE [SportID]=" & CLng(Session("MySport")) & " Order by [GameTime];"
rstimes.open SQL_query, DataConn
if rstimes.EOF = FALSE then
rstimes.MoveFirst
end if
%>
<SELECT class=tbFlat NAME="<%= "Time2(" & i &")" %>" SIZE=1 onchange="alert('Your selection of a Time here will supercede any text entered in the Time text box. To manually enter a Time, select the blank space at the top of the list of times and enter the time in the text box below.')">
<%
Response.Write "<OPTION SELECTED VALUE='" & " " & "'>"
Response.Write " " & "</OPTION>"
Do While NOT rstimes.EOF
if (rstimes("GameTime") = rs.Fields("Time").Value) then
Response.Write "<OPTION SELECTED VALUE='" & TrimSeconds(replace(rstimes("GameTime"),"'","&#39")) & "'>"
Response.Write TrimSeconds(rstimes("GameTime")) & "</OPTION>"
else
Response.Write "<OPTION VALUE='" & TrimSeconds(replace(rstimes("GameTime"),"'","&#39")) & "'>"
Response.Write TrimSeconds(rstimes("GameTime")) & "</OPTION>"
end if
rstimes.MoveNext
Loop
rstimes.Close
%>
</SELECT>


<input type=text class=tbFlat name="<%= "Time(" & i &")" %>" maxLength=10 size=10 onchange="alert('To use a manually entered Time, make sure and clear the Time from the select box above.')" value ="<%= TrimSeconds(gametime(i)) %>"/><br />


</td>
<td>
<%
set rslevels=server.CreateObject("ADODB.Recordset")
SQL_query = "SELECT [Level] FROM Levels WHERE [SportID]=" & CLng(Session("MySport")) & " Order by [Level];"
rslevels.open SQL_query, DataConn
if rslevels.EOF = FALSE then
rslevels.MoveFirst
end if
%>
<SELECT class=tbFlat NAME="<%= "Level(" & i & ")" %>" SIZE=1>
<%
' put in a blank option
Response.Write "<OPTION SELECTED VALUE='" & 0 & "'>"
Response.Write " " & "</OPTION>"
Do While NOT rslevels.EOF

if (rslevels("Level") = rs.Fields("Level").Value) then
Response.Write "<OPTION SELECTED VALUE='" & replace(rslevels("Level"),"'","&#39") & "'>"
Response.Write rslevels("Level") & "</OPTION>"
else
Response.Write "<OPTION VALUE='" & replace(rslevels("Level"),"'","&#39") & "'>"
Response.Write rslevels("Level") & "</OPTION>"
end if
rslevels.MoveNext
Loop
rslevels.Close
%>
</SELECT>
</td>
<td>

<%
command = "form1.Visitor2(" & i & ").value=0"
set rsteams=server.CreateObject("ADODB.Recordset")
SQL_query = "SELECT [ID], [SchoolName] FROM Schools WHERE [SportID]=" & CLng(Session("MySport")) & " Order by SchoolName;"
rsteams.open SQL_query, DataConn
rsteams.MoveFirst
%>
<SELECT class=tbFlat NAME="Visitor2(<%=i%>)" onchange="form1.Visitor(<%=i%>).value=' '" SIZE=1>
<%

' Response.Write "<OPTION SELECTED VALUE='" & " " & "'>"

Response.Write "<OPTION SELECTED VALUE=0>"

Response.Write " " & "</OPTION>"

Do While NOT rsteams.EOF

if (rsteams("ID") = rs.Fields("VisitorTeamID").Value) then

' Response.Write "<OPTION SELECTED VALUE=""" & replace(rsgames("SchoolName"),"'","&#39") & """>"

Response.Write "<OPTION SELECTED VALUE='" & rsteams("ID") & "'>"

Response.Write rsteams("SchoolName") & "</OPTION>"

else

' Response.Write "<OPTION VALUE=""" & replace(rsgames("SchoolName"),"'","&#39") & """>"

Response.Write "<OPTION VALUE='" & rsteams("ID") & "'>"

Response.Write rsteams("SchoolName") & "</OPTION>"

end if

rsteams.MoveNext

Loop

%>

</SELECT>

<B>

OR

</B>

<input type=text name="Visitor(<%=i%>)" maxLength=50 size=35 onchange="form1.Visitor2(<%=i%>).value=0"

value="<%= rs.Fields("VisitorTeamName").Value %>" /><br />



</td>

<td>
<input type="text" name="<%="Location(" & i & ")" %>" onkeypress='return anyMask(event, "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");' SIZE=50 MAXLENGTH=50 value ="<%= loc(i) %>"/><br />
</td>

<td>
<input align=left type="checkbox" name="<%= "Scrimmage(" & i & ")" %>" <%if (rs.Fields("Scrimmage").Value) then Response.Write " CHECKED" end if%> />
</td>



</tr>



<%


rs.MoveNext

Next ' i = 0 to numrecs-1


end if ' if (numrecs > 0)


%>


<tr>

&nbsp;

</tr>

<tr>

&nbsp;

</tr>

<tr>

&nbsp;

</tr>

<tr>

&nbsp;

</tr>

<tr>


<td colspan=7>
<input align=right TYPE="submit" VALUE="Save Schedule" id=submit1 name=submit1>
</td>


</tr>



</form>
Copy linkTweet thisAlerts:
@russellNov 18.2004 — ok, your html should look something like this (my very short example):
<i>
</i>&lt;html&gt;
&lt;script&gt;&lt;!--
function clearText(i) {
el = eval("document.scheduleForm.opponent" + i);
el.value= "";
}
// --&gt;
&lt;/script&gt;
&lt;form name="scheduleForm"&gt;
&lt;select name=team1 onchange="clearText(1)"&gt;
&lt;option value=""&gt;Choose Opponent&lt;/option&gt;
&lt;option&gt;Bears&lt;/option&gt;
&lt;option&gt;Vikings&lt;/option&gt;
&lt;option&gt;Packers&lt;/option&gt;
&lt;option&gt;Lions&lt;/option&gt;
&lt;/select&gt;
&lt;input type=text name=opponent1 value="Bears"&gt;
&lt;br&gt;

&lt;select name=team2 onchange="clearText(2)"&gt;
&lt;option value=""&gt;Choose Opponent&lt;/option&gt;
&lt;option&gt;Raiders&lt;/option&gt;
&lt;option&gt;Broncos&lt;/option&gt;
&lt;option&gt;Chargers&lt;/option&gt;
&lt;option&gt;Chiefs&lt;/option&gt;
&lt;/select&gt;
&lt;input type=text name=opponent2 value="Raiders"&gt;
&lt;/form&gt;
&lt;/html&gt;

You'd generate that in asp like so:
<i>
</i>&lt;%
With Response
While Not rs.Eof
id = rs("team_id")
.Write "&lt;select name=team" &amp; id &amp; " onchange=""clearText(" &amp; id &amp; ")""&gt;" &amp; vbCrLf
'' write options here
.Write "&lt;/select&gt;" &amp; vbCrLf
.Write "&lt;input type=""text"" name=opponent" &amp; id &amp; " value=" &amp; someValueHere &amp; "&gt;" &amp; vbCrLf
.Write "&lt;br&gt;" &amp; vbCrLf
rs.MoveNext
Wend
End With
%&gt;
×

Success!

Help @kball1958 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...