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

Weird Problem with onChange Event 1

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Guys,

I have the following code in a HTML page. When the user selects an address from a drop down list then I want to change the value of a hidden variable. However, when I do this I get a JS error saying that "Object does not support this property or Method". The line the error refers to is the line where I have the select statement with the onChange event.

Can you please take a look at the code below and see if you can spot any errors.


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function shipCode(shipaddr,shipCode) {
var select = eval(shipaddr.selectedIndex);
var catId = shipaddr.options[select].value;
var d = shipCode;
if (catId == 'STREET1, CITY1, COUNTRY1'){
d.value='ADDRESS1';
}
else if (catId == 'STREET2, CITY2, COUNTRY2'){
d.value='ADDRESS2';
}
} // end function
//-->
</SCRIPT>
</HEAD>
<BODY&quot;>
<FORM action=&quot;placeorder2.asp&quot; method=&quot;post&quot; name=&quot;OrderForm&quot; onSubmit=&quot;return validate()&quot;>
<TABLE CELLPADDING=2 CELLSPACING=2 WIDTH='100%'>
</TR>
<TR>
<TD ALIGN=LEFT WIDTH=120 VALIGN=TOP><b>Ship To Address:</b></TD>
<TD ALIGN=LEFT><SELECT NAME=&quot;shiptoaddr&quot; onChange=&quot;shipCode(document.OrderForm.shiptoaddr, document.OrderForm.shipCode)&quot;><OPTION VALUE=&quot;STREET1, CITY1, COUNTRY1&quot;>STREET1, CITY1, COUNTRY1</OPTION>
<OPTION VALUE=&quot;STREET2, CITY2, COUNTRY2&quot;>STREET2, CITY2, COUNTRY2</OPTION>
</SELECT></TD></TR>
</TABLE>

<INPUT TYPE = &quot;HIDDEN&quot; NAME = &quot;shipCode&quot; VALUE = &quot;ORBUS&quot;>
<TABLE>
<TR>
<TD ALIGN=CENTER COLSPAN=2><INPUT type=&quot;Submit&quot; value=&quot;Proceed to Stage 2 -->&quot; name=&quot;Submit&quot;></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Mise Le Meas,

Mighty :)
 
might have been a namespace cofnlict, changed the offending line to:

<SELECT NAME=&quot;shiptoaddr&quot; onChange=&quot;shipCodeChange(this,this.form.shipCode)&quot;>

and changed the function's name to shipCodeChange jared@eae.net -
 
jaredn,

Thanks.
It was just a simple name conflict problem. I changed the name of the function to shipCodeChange and it worked fine. Is there any advantage to use &quot;this&quot; instead of &quot;document.OrderForm.shiptoaddr&quot; ?? Mise Le Meas,

Mighty :)
 
Yes, each time you traverse a node in the document hierarchy, it takes more time. In this case, the increase in speed is not significant, but it is good practice (and it saves you some keystrokes :) ) jared@eae.net -
 
The namespace conflict you came up with is a good warning for everyone. IE (at least) does not like forms, fields and/or functions to have the same name. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon, yes, but ...if you adress them the &quot;normal&quot; way
because you can name all your forms &quot;forms&quot; and all their elements &quot;input&quot; and it'll still work, depending on how you adress them (document.forms[index].... instead of document.formname ...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top