where am i going wrong? (javascript)

Status
Not open for further replies.

Leaping

Smack-Fu Master, in training
96
Im trying to get a database sorted out but I seem to be having a problem.

The following is the problem boiled down to a one page html document, the idea being that the document is able to re-write and display different forms for user selection as the user chooses different options..

<html>
<head>
<script>
function writeForm(whichOption)
{
top.document.open();
top.document.write("<body bgcolor=navy text=white>");
top.document.write("<form name='List1Form'>");
top.document.write("Main List :
");
top.document.write("<select name='List1Item'>");
top.document.write("<option value=1>a</option>");
top.document.write("<option value=2>b</option>");
top.document.write("</select></form>");
top.List1Form.List1Item.value=whichOption;
top.document.write("</body>");
top.document.close();
}
</script>
</head>
<body>

<script>writeForm(2);</script>

<script>writeForm(2);</script>

</body>
</html>

The above works fine with only one <script>writeForm(2);</script> line in it, but add a second and you get:

Error:'top.List1Form.List1Item' is null or not an object.

If you remove the top.List1Form.List1Item.value=whichOption; line it all works fine so the problem seems to be the above line. I kinda get the feeling it's to do with trying to write a new form obejct which in effect already exists. Is this correct? If so how would I go about fixing this, do I have to kill off the form before or something? How do I do this? Am I trying to do this the wrong way?

Clarification would be appreciated, me confused :)
 

JHS

Ars Tribunus Militum
2,742
Subscriptor++
You want to set one of the options (<option> ) as selected based on the value you submit to the function. You'll need to loop through the options, comparing each option's value with the value you submitted, when you find a match, you then set that option's selected property to true.

code:<HR>
Code:
<html>
<head>
<script>
  function writeForm(whichOption)
  {
    top.document.open();
    top.document.write("<body bgcolor=navy text=white>");
    top.document.write("<form name='List1Form'>");
    top.document.write("Main List :
");
    top.document.write("<select name='List1Item'>");
    top.document.write("<option value=1>a</option>");
    top.document.write("<option value=2>b</option>");
    top.document.write("</select></form>");
    for (var i=0; i<top.List1Form.List1Item.options[]; i++){
      if (top.List1Form.List1Item.options[i].value == whichOption){
        top.List1Form.List1Item.options[i].selected = true;
        break;
      }
    }
    top.document.write("</body>");
    top.document.close();
  }
</script>
</head>
<body>
<script>writeForm(2);</script><P><script>writeForm(2);</script><P></body>
</html>

EDIT: I should've checked what I wrote before posting. I now see that your problem lies with how you named the form. Since you're writing the forms to the same document, the second time through the function, a second form is written with the same name as the first. So when your code tries to set the value of the select box, it doesn't know which one to refer to since they are both named the same. Try changing the form name the second time through, or writing to a new window each time through.

I'm still not sure if your "top.List1Form.List1Item.value=whichOption;
" will work properly.

[This message was edited by JHS on October 16, 2001 at 18:34.]
 

Leaping

Smack-Fu Master, in training
96
I think probably I worder my queston badly and also it probably a bad example, twas late, it does simplify/illustrate the problem I'm having though.

What I actually want is the above example to function properly.

The problem is that the form is being written twice to the frame, I need a way nuke the frame before writing the second form to the frame, ie to wipe the first form completely from the frame so the 2nd one will successfully write.

Do I not need to document.write the body tag out cupcake?
 
Status
Not open for further replies.
\n