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

Select OnChange Issue

Status
Not open for further replies.

rza22gza

Programmer
Feb 20, 2002
9
0
0
US
I have two selects that are dynamically created and I want both onChange events to call the same function. This works fine the first time I change an option, but when I select a second time, I get a RunTimeError "Object Required". I have no idea what the issue is. Is it possible to have 2 selects that use the same function in the onChange event handler? Please help. Thanks.

Jacob
 
Here is an example of the code.......

<html>
<head><title>Test</title>

<script language=&quot;JavaScript&quot;>

function createForm()
{
document.clear();
document.open();
document.writeln(&quot;<form name='test'>&quot;);
document.writeln(&quot;<table><tr>&quot;);
document.writeln(&quot;<td><select name=&quot;Chapter&quot; onChange='javascript:selectChange();&quot;</td></tr>&quot;);
document.writeln(&quot;<td><select name=&quot;Section&quot; onChange='javascript:selectChange();&quot;</td></tr></table>&quot;);
document.writeln(&quot;</form>&quot;);
document.close();

loadOptions();
}

function loadOptions()
{
//this is where I put code to fill the selects that were created in the other function

}
function selectChange()
{
alert(&quot;This is where it should go after a select change has been made for either select&quot;);

}

</script>

</head>
<body>
<form name=&quot;test>
<script language=&quot;JavaScript&quot;>
createForm();
</script>
</form>
</body>
</html>
 
Unterminated String Constant.

Where you have a String enclosed in double-quotes, make sure the string's contents don't contain any.


document.writeln(&quot;<td><select name=&quot;Chapter&quot; onChange='javascript:selectChange();&quot;</td></tr>&quot;);
document.writeln(&quot;<td><select name=&quot;Section&quot; onChange='javascript:selectChange();&quot;</td></tr></table>&quot;);
 
apologies. I was writing very fast and I know that all the code inside the double-quotes are single-quotes. When I load with this type of code, it loads fine and shows both selects with all available options. Then I change the first select to a different option, and it works fine, but if I try to change the option a second time that is when I get a RunTime Error &quot;Object Expected.&quot; I have no idea what the problem is. Do you have any other remedies or thoughts of what might be going on. Thanks a million for your help.
 
Here you go.... This loads fine but when I try to change a select option it crashes (Object Expected). If I try in Netscape the error message says, &quot;function selectChange is not defined.&quot; Very strange, please help. Thanks again.

<html>
<head>
<title>Test Selects</title>
<script language=&quot;JavaScript&quot;>

function selectChange(elementIndex)
{
var selectedOption = &quot;&quot;;

selectedOption = document.jacob.elements[elementIndex].options[document.jacob.elements[elementIndex].options.selectedIndex].value
alert(&quot;Selected Option: &quot; + selectedOption + &quot; for &quot; + document.jacob.elements[elementIndex].id)
}

function createForm()
{
document.clear();
document.open();
document.writeln(&quot;<form name='jacob' action=''>&quot;)
document.writeln(&quot;<select name='Select1' onChange='selectChange(0)' id='Select1'>&quot;)
document.writeln(&quot;<option value='Option 1'>Option 1&quot;)
document.writeln(&quot;<option value='Option 2'>Option 2&quot;)
document.writeln(&quot;<option value='Option 3'>Option 3&quot;)
document.writeln(&quot;</select>&quot;)
document.writeln(&quot;<br><br>&quot;)
document.writeln(&quot;<select name='Select2' onChange='selectChange(1)' id='Select2'>&quot;)
document.writeln(&quot;<option value='Option 4'>Option 4&quot;)
document.writeln(&quot;<option value='Option 5'>Option 5&quot;)
document.writeln(&quot;<option value='Option 6'>Option 6&quot;)
document.writeln(&quot;</select>&quot;)
document.writeln(&quot;</form>&quot;)
document.close();
}

</script>
</head>

<body onload=&quot;createForm()&quot;>
</body>
</html>
 
change
<body onload=&quot;createForm()&quot;>
</body> to
<body>
<script>createForm()</script>
</body>
and it should work
 
Hi all,

It works just fine like this:

<html>
<head>
<title>Test Selects</title>
<script language=&quot;JavaScript&quot;>
<!--

function selectChange(elementIndex)
{
var selectedOption = &quot;&quot;;
selectedOption = document.jacob.elements[elementIndex].options[document.jacob.elements[elementIndex].options.selectedIndex].value;
alert(&quot;Selected Option: &quot; + selectedOption + &quot; for &quot; + document.jacob.elements[elementIndex].id);
}

//-->
</script>
<body>

<script language=&quot;JavaScript&quot;>
<!--

document.clear();
document.open();
document.writeln('<body>');
document.writeln('<form name=&quot;jacob&quot; action=&quot;&quot;>');
document.writeln('<select name=&quot;Select1&quot; onChange=&quot;selectChange(0)&quot; id=&quot;Select1&quot;>');
document.writeln('<option value=&quot;Option 1&quot;>Option 1');
document.writeln('<option value=&quot;Option 2&quot;>Option 2');
document.writeln('<option value=&quot;Option 3&quot;>Option 3');
document.writeln('</select>');
document.writeln('<br><br>');
document.writeln('<select name=&quot;Select2&quot; onChange=&quot;selectChange(1)&quot; id=&quot;Select2&quot;>');
document.writeln('<option value=&quot;Option 4&quot;>Option 4');
document.writeln('<option value=&quot;Option 5&quot;>Option 5');
document.writeln('<option value=&quot;Option 6&quot;>Option 6');
document.writeln('</select>');
document.writeln('</form>');
document.writeln('</body>');
document.close();

//-->
</script>

</body>
</html>


I noticed that when the formCreate function fired, it would
first whipe out all text in the document and then write the
lines (dont ask why, i havent got a clue), therefore the
selectChange function siezed to exist, giving you an error.
When you just write the lines straight away, the clearing of
of the code does not happen and then you are fine!

I hope this helps, BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com

&quot;<beer brand> is like making love in a cano...
it's <f-word + ing> close to water !!!&quot;
- Monty Python's Flying Circus, 1969/70
 
BobbaFet: Your code crashes IE. This is because IE doesn't like it when you
Code:
document.close()
in a script that is inside the document. Getting rid if
Code:
document.close()
will make your code just fine.
bluebrain.gif
blueuniment.gif
 
Well, I only tested it in IE 6.0, and didnt give me
any errors. But thanks anyway, UNIMENT. BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com

&quot;<beer brand> is like making love in a cano...
it's <f-word + ing> close to water !!!&quot;
- Monty Python's Flying Circus, 1969/70
 
hmm. IE didn't give me any errors either (except freezing and requiring a force-quit). And I'm using IE 6.0

Or maybe it's just because of my usage of HTMaker to test scripts. Either way, it's still not a good idea to
Code:
document.close()
within the document.
bluebrain.gif
blueuniment.gif
 
Why not? It never gave me any problems before. BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com

&quot;<beer brand> is like making love in a cano...
it's <f-word + ing> close to water !!!&quot;
- Monty Python's Flying Circus, 1969/70
 
Hi UNIMENT,

Put the bolded text in a new document and you'll see that
it works just fine. And by the way, NEVER EVER EVER test
something itself (dont use javascript to test javascript).
(and at that its a shitty tester, too)

I hope you'll see the light ;-) BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Using my &quot;shitty&quot; tester is a lot faster than saving a file to your hard drive. And besides that, for most applications, it works exactly the same way as a saved file.

Besides that, it's best to be safe.
bluebrain.gif
blueuniment.gif
 
Yes my point exactly!

That's why its better to either save it to file or
use another program to test. And if you really want to
do it that way, at least you should use JavaScript's
Eval() function. Or write the lines away to a popup window.

Greetz, BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Bah... whatever.

I still think using frames is better than the popup testers -- more convenient, but with the same reliability.
bluebrain.gif
blueuniment.gif
 
Actually, the inconsistency you have witnessed here today is the result of having a page that is generated dynamically. If you had a popup tester, the same problem would have occurred.
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top