Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

selectedIndex always 0 in document.writeln()

I have a select menu with an onChange= that calls a function that sets some cookie values and reloads the page.  Right after this select, I want to access the selectedIndex but when I do document.writeln(document.myForm.app.selectedIndex), it always outputs 0.  However, if I have a link like <a href=javascript:alert(document.myForm.app.selectedIndex)>, it gives the right value.  How do I get document.writeln to output the right selectedIndex???
0
AmiraliR
Asked:
AmiraliR
  • 10
  • 8
1 Solution
 
fritz_the_blankCommented:
You can try storing the selected index to a variable, and then write out the variable. I think the issue is that when you do the document.writeln() it may zap your page.

intSelected = document.myForm.app.selectedIndex;
document.writeln(intSelected)

Fritz the Blank
0
 
AmiraliRAuthor Commented:
No, that didn't work.  I don't think document.writeln is the problem.  Even if I do alert(document.myForm.app.selectedIndex) it is always 0.  But if I use a link instead like I said before - <a href=javascript:alert(document.myForm.app.selectedIndex) - and then click on the hyperlink it works correctly.
0
 
AmiraliRAuthor Commented:
No, that didn't work.  I don't think document.writeln is the problem.  Even if I do alert(document.myForm.app.selectedIndex) it is always 0.  But if I use a link instead like I said before - <a href=javascript:alert(document.myForm.app.selectedIndex) - and then click on the hyperlink it works correctly.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
fritz_the_blankCommented:
Hang on, if you reload the page, won't the selectedIndex be 0?

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Yes it would be 0, except (I forgot to mention) that the selectedIndex is saved in a cookie and then when the page reloads, the select menu uses this to set its selected index.  This is why the javascript:alert() link works correctly.  But for some reason the other way doesn't work ... pretty baffling to me.
0
 
fritz_the_blankCommented:
Okay, so you're saying that when the page reloads, the index of the select is dynammically reset based on the value of the cookie?

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Yup
0
 
fritz_the_blankCommented:
Well, you could always write out the value from the cookie rather than from the select control....

Perhaps the select control doesn't register the change until it gets focus or something like that.

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Yes, I could use the value from the cookie directly ... but this is bugging the hell out of me!  Why would

<a href=javascript:alert(document.myForm.app.selectedIndex)>

generate an alert with the correct index when the link is clicked on but

<script LANGUAGE="JavaScript>
alert(document.myForm.app.selectedIndex)
</script>

will generate an alert displaying 0 every single time???
0
 
AmiraliRAuthor Commented:
correction: <script LANGUAGE="JavaScript"> (no quote is missing in my code)

by the way Fritz, thanks for sticking with this for this long
0
 
fritz_the_blankCommented:
When do you call this:

<script LANGUAGE="JavaScript">
alert(document.myForm.app.selectedIndex)
</script>

Is there any chance that the page has not finished loading yet? Clearly, by the time that you can click on the hyperlink, the page is rendered. What if you call the above code from a link or a button?

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Well, the select menu is in a table cell and I call that code in the next cell.  alert or document.writeln both generate a selectedIndex of 0.  In fact, I'd have the link and the document.writeln side by side like this:

<tr><td><a href=javascript:alert(document.myForm.app.selectedIndex)>Click here</a><br>
<script LANGUAGE="JavaScript">
 document.writeln(document.myForm.app.selectedIndex)
</script></tr></td>

So it shows up as a "Click here" link and then the number 0.  But if I click on "Click here", it would alert with the correct selectedIndex.
0
 
AmiraliRAuthor Commented:
Correction:  </td></tr>, not </tr></td> ...
0
 
fritz_the_blankCommented:
I am thinking that the select is not fully rendered  (or atleast its properties are not registered) before the code is called, and that is why it is not working.

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Hmmm ... I think I sort of see what you're saying.  Let me mess around with the code a little.  Thanks

- Amir
0
 
fritz_the_blankCommented:
The idea is that all of the properties of all of the objects might not be set until the page is finished loading. If you are calling your code before then, you might run into inconsistancies. That you can call the same code via a button suggests that there is nothing wrong with your code. What if you do:

<body onLoad="alert(document.myForm.app.selectedIndex)">

Fritz the Blank
0
 
AmiraliRAuthor Commented:
Yup ... I get it.  Thanks again!
0
 
fritz_the_blankCommented:
Glad to have helped. I am sorry that it didn't work out quite right.

Fritz the Blank
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now