JCWright30
asked on
OnChange Event for DropDown List
I am having a problem... I have an access database and an ASP application. On my add screen I am populating a dropdown box with a field from the database. I want to use the OnChange event to fill in a textbox below with the corresponding information. I am combining HTML, ASP, and VBScript for my Onchange event.
I believe the easiest way to achieve this is to create an array. I receive two JS Errors when I run this page
the first error is expected end (this occurs at the end of this line: var pubArray = new Array()
Then I get this error object undefined. (this occurs at this line: <option></option>)
Below is my code! Can somebody please help? I have lost hours on this and I would really appreciate any input.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
objRS.Open "tblJournalNames", objDC, 0, 1
If Not objRS.EOF Then
objRS.MoveFirst
%>
<script type="text/vbscript" language="vbscript">
var pubArray = new Array()
function applytoLabel(a)
document.Form1.txtJournal. value = a.value
document.Form1.txtAuthor.V alue = pubArray(a.SelectedIndex)
end function
</script>
<select name="comboJournal" onChange="applytoLabel(thi s)">
<option></option>
<%
x = 0
Do While Not objRS.EOF
%>
<script language="vbscript">
pubArray<%= x %> = "<%= objRs.Fields("Pub_Code").V alue %>"
</script>
<option value="<%= objRS.Fields("Journal_Name ") %>"><%= objRS.Fields("Journal_Name ") %></option>
<%
objRS.MoveNext
Loop
%>
</select>
<font face="Tahoma">
<% end if %>
I believe the easiest way to achieve this is to create an array. I receive two JS Errors when I run this page
the first error is expected end (this occurs at the end of this line: var pubArray = new Array()
Then I get this error object undefined. (this occurs at this line: <option></option>)
Below is my code! Can somebody please help? I have lost hours on this and I would really appreciate any input.
--------------------------
objRS.Open "tblJournalNames", objDC, 0, 1
If Not objRS.EOF Then
objRS.MoveFirst
%>
<script type="text/vbscript" language="vbscript">
var pubArray = new Array()
function applytoLabel(a)
document.Form1.txtJournal.
document.Form1.txtAuthor.V
end function
</script>
<select name="comboJournal" onChange="applytoLabel(thi
<option></option>
<%
x = 0
Do While Not objRS.EOF
%>
<script language="vbscript">
pubArray<%= x %> = "<%= objRs.Fields("Pub_Code").V
</script>
<option value="<%= objRS.Fields("Journal_Name
<%
objRS.MoveNext
Loop
%>
</select>
<font face="Tahoma">
<% end if %>
ASKER
Here is the fix. This works well and I wanted to post if for others having similar problems.
objRS.Open "tblJournalNames", objDC, 0, 1
If Not objRS.EOF Then
objRS.MoveFirst
%>
<script type="text/vbscript" language="vbscript">
dim pubArray()
function applytoLabel(a)
if a.SelectedIndex = 0 then
document.Form1.txtJournal. value = ""
document.Form1.txtAuthor.V alue = ""
else
document.Form1.txtJournal. value = a.value
document.Form1.txtAuthor.V alue = pubArray(a.SelectedIndex - 1)
end if
end function</script>
<select name="comboJournal" onChange="applytoLabel(thi s)">
<option></option>
<%
x = 0
Do While Not objRS.EOF
%>
<script language="vbscript">
ReDim Preserve pubArray(<%= x %>)
pubArray(<%= x %>) = "<%= objRs.Fields("Pub_Code").V alue %>"
</script>
<option value="<%= objRS.Fields("Journal_Name ") %>"><%= objRS.Fields("Journal_Name ") %></option>
<%
x = x + 1
objRS.MoveNext
Loop
%>
</select>
<font face="Tahoma">
<% end if %>
objRS.Open "tblJournalNames", objDC, 0, 1
If Not objRS.EOF Then
objRS.MoveFirst
%>
<script type="text/vbscript" language="vbscript">
dim pubArray()
function applytoLabel(a)
if a.SelectedIndex = 0 then
document.Form1.txtJournal.
document.Form1.txtAuthor.V
else
document.Form1.txtJournal.
document.Form1.txtAuthor.V
end if
end function</script>
<select name="comboJournal" onChange="applytoLabel(thi
<option></option>
<%
x = 0
Do While Not objRS.EOF
%>
<script language="vbscript">
ReDim Preserve pubArray(<%= x %>)
pubArray(<%= x %>) = "<%= objRs.Fields("Pub_Code").V
</script>
<option value="<%= objRS.Fields("Journal_Name
<%
x = x + 1
objRS.MoveNext
Loop
%>
</select>
<font face="Tahoma">
<% end if %>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I may be missing the point but instead of <option></option> could you have <option value=-1></option>?
This should hopefully ensure that the object is defined as I beleive that an option object is defined if it has a value....
Hope this helps