[thelist] Javascript Error

Matt Warden mwarden at gmail.com
Tue Dec 21 16:21:36 CST 2004


On Tue, 21 Dec 2004 16:26:22 -0500, Norman Bunn
<norman.bunn at craftedsolutions.com> wrote:
> <script language="JavaScript" type="text/javascript">
>     function build_string() {
>         var myIndex = '-1';
>         var search_string = '';
>         for (element in window.document.megasearch) {
>             myIndex = window.document.megasearch[element].selectedIndex;
>             if (myIndex > 0) {
>                 if (window.document.megasearch[element].name == 'pot_size'){
>                     search_string = search_string + ' ' +
> window.document.megasearch[element][myIndex].value + '\"';
>                 }
>                 else {
>                     search_string = search_string + ' ' +
> window.document.megasearch[element][myIndex].value;
>                 }
>                 window.document.megasearch.Search.value = search_string;
>             }
>             myIndex = '-1';
>         }
>     }
> </script>

Norman,

There are a number of problems with your script. I will take a stab at
it, although I'm not certain I understand what you're trying to do.

First, this line:

for (element in window.document.megasearch) {

You are currently trying to iterate over a form object. This doesn't
make sense. I assume you are trying to iterate over the elements in
the form. For taht, you need something like this:

for (element in window.document.megasearch.elements) {

So, not element references an element in the elements array of the
form. Therefore,

myIndex = window.document.megasearch[element].selectedIndex;

Ought to become:

myIndex = window.document.megasearch.elements[element].selectedIndex;

myIndex refers now to the selected index of the form element. This
doesn't make sense for anything but a select box, but I'll leave that
be. That said, this line is incorrect as well:


search_string = search_string + ' ' +
window.document.megasearch[element][myIndex].value;

This should become

search_string = search_string + ' ' +
window.document.megasearch.elements[element].options[myIndex].value;

or, equivalently, and maybe a little easier to read line-wrapped:

search_string +=  ' ';
search_string += document.megasearch.elements[element].options[myIndex].value;

This way, you are accessing the option object with the index equal to
the selected index (i.e., the selected option) and getting its value
property.



-- 
Matt Warden
Miami University
Oxford, OH
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list