?
Solved

selectedIndex always 0 in document.writeln()

Posted on 2003-03-26
18
Medium Priority
?
250 Views
Last Modified: 2012-06-21
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
Comment
Question by:AmiraliR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8212612
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
 

Author Comment

by:AmiraliR
ID: 8212757
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
 

Author Comment

by:AmiraliR
ID: 8212769
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8212844
Hang on, if you reload the page, won't the selectedIndex be 0?

Fritz the Blank
0
 

Author Comment

by:AmiraliR
ID: 8213115
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8213216
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
 

Author Comment

by:AmiraliR
ID: 8213249
Yup
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8213271
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
 

Author Comment

by:AmiraliR
ID: 8213365
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
 

Author Comment

by:AmiraliR
ID: 8213379
correction: <script LANGUAGE="JavaScript"> (no quote is missing in my code)

by the way Fritz, thanks for sticking with this for this long
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8213403
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
 

Author Comment

by:AmiraliR
ID: 8213455
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
 

Author Comment

by:AmiraliR
ID: 8213479
Correction:  </td></tr>, not </tr></td> ...
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8213510
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
 

Author Comment

by:AmiraliR
ID: 8213685
Hmmm ... I think I sort of see what you're saying.  Let me mess around with the code a little.  Thanks

- Amir
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 300 total points
ID: 8213809
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
 

Author Comment

by:AmiraliR
ID: 8214110
Yup ... I get it.  Thanks again!
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8214140
Glad to have helped. I am sorry that it didn't work out quite right.

Fritz the Blank
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question