473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select Option & selected

Hello all,

I'm currently writing a tiny little bit of navigation to a page, and
I've come across this stumbling block:

Goto Page  
<select name="page">
<SCRIPT LANGUAGE="JavaScript1.2" >
for ( var inc = 1; inc <= <%=nPages%>; inc++ ){
document.PageForm.page[inc-1] = new Option(inc, inc, ((inc ==
<%=nPage%>) ? true : false));
}
</SCRIPT>
</select>

On firefox, the select box generated behaves exactly as expected,
producing a select with all the 'pages' available, and the currently
selected page as the selected item. On IE6, however, it only generates
a select box with the pages, the selected item always at the first
item.

Am I doing this the wrong way? Is there something else I should
consider trying?

Thanks,

Brian

Jul 23 '05 #1
7 2276
brian.ackermann wrote:

Hi Brian,
Hello all,

I'm currently writing a tiny little bit of navigation to a page, and
I've come across this stumbling block:

Goto Page&nbsp;&nbsp;
<select name="page">
<SCRIPT LANGUAGE="JavaScript1.2" >
Try to avoid that ancient bad script-tag
Use:
<script type="text/javascript">
for ( var inc = 1; inc <= <%=nPages%>; inc++ ){
document.PageForm.page[inc-1] = new Option(inc, inc, ((inc ==
<%=nPage%>) ? true : false));
}
</SCRIPT>
</select>

On firefox, the select box generated behaves exactly as expected,
producing a select with all the 'pages' available, and the currently
selected page as the selected item. On IE6, however, it only generates
a select box with the pages, the selected item always at the first
item.

Am I doing this the wrong way? Is there something else I should
consider trying?
Well, I would just rewrite the code, avoiding JS.
ASP/VBScript, right?

<select name="page">
<%
Dim pageCount
Dim SELECTEDOPTION
for pagecount=1 to nPages
SELECTEDOPTION = ""
If (pagecount = nPages) Then
SELECTEDOPTION = " SELECTED "
End If
%>
<OPTION value="<%= NotSureIThinkYouKnow %>" <%= SELECTEDOPTION %> >
<%= pageCount %>
<%
Next
%>
</select>
Something like that.

Regards,
Erwin Moller

Thanks,

Brian


Jul 23 '05 #2
The problem in ASP (which made me try a JS option) was that my

If (pagecount = nPages) Then

never, EVER, was true inside the loop. Outside the loop it was
fine....very confusing to me. So, I went with JS, and at least that is
working, on firefox.

Grr.

Thanks though :)

Jul 23 '05 #3
brian.ackermann wrote:
The problem in ASP (which made me try a JS option) was that my

If (pagecount = nPages) Then

never, EVER, was true inside the loop. Outside the loop it was
fine....very confusing to me. So, I went with JS, and at least that is working, on firefox.

Grr.

Thanks though :)


The Option() constructor actually takes *four* arguments, the third of
which is 'default selected'; in point of fact, what you really need is
the fourth one, 'selected', as IE doesn't appear to reset to the
default just because a new one is specified (seems like Explorer's
behavior is the more logical).

<select name="page">
<script type="text/javascript">

for (var inc = 1, opts = document.PageForm.page.options; inc <= 10;
inc++ )
{
opts[inc-1] = new Option(inc, inc, null, inc == 6);
}

</script>

Hardcoded in those asp vars, you do the math...no need to embed this
'inside' the select, btw, as you're not document.write()ing anything...

Jul 23 '05 #4
True, that, its just there because I WAS document.write(ing) the first
couple ways I tried to fix this.

This definately gets me closer. In testing, I found that applying your
changes (RobB) that firefox continues to behave as I desire, and now IE
almost does, though it is off by one.

For example, if I go to page 20, Firefox's select shows 20, and IE's
shows 19. Still a bit confusing, but its closer now. Any other ideas?

Brian

Jul 23 '05 #5

brian.ackermann wrote:
True, that, its just there because I WAS document.write(ing) the first couple ways I tried to fix this.

This definately gets me closer. In testing, I found that applying your changes (RobB) that firefox continues to behave as I desire, and now IE almost does, though it is off by one.

For example, if I go to page 20, Firefox's select shows 20, and IE's
shows 19. Still a bit confusing, but its closer now. Any other ideas?
Brian


Take your pick...

<html>
<head>
<title>foo</title>
<script type="text/javascript">

window.onload = function()
{
for (var i = 0, opts = document.PageForm.page.options; i <
<%=nPages%>; ++i)
opts[i] = new Option(i + 1, i + 1, null, i == <%=nPage%>);
}

</script>
</head>
<body>
<form name="PageForm">
<select name="page">
</select>
<select>
<script type="text/javascript">
for (var i = 0; i < <%=nPages%>; ++i)
document.write(
'<option value="' ,
i + 1 ,
'"' ,
(i == <%=nPage%>) ? ' selected' : '' ,
'>' ,
(i + 1) ,
'</option>'
);

</script>
</select>
</form>
</body>
</html>

IE was setting the selected option inaccurately, probably as a result
of it having just been created.

Jul 23 '05 #6
the second method is what I had been doing to begin with.

What was happening was a tag like

<option 17 selected="selected"> 17 </option>

Which was NOT loading up as a default, it would always stay at "1". I
tried figuring out where that extra ="selected" was coming
from...probably asp was munging it as it went out for whatever reason.
Bottom line, it didn't work.

I'd never even have bothered with the javascript if the asp code would
have worked right off. I still have no Idea why the expression never
returned true inside a loop, while outside a loop it would evaluate as
expected.

Brian

Jul 23 '05 #7
brian.ackermann wrote:
What was happening was a tag like

<option 17 selected="selected"> 17 </option>
Which was NOT loading up as a default, it would always stay at "1".
Because it is invalid. You might want

<option value="17" selected>17</option>

Check out <http://validator.w3.org/>!
I tried figuring out where that extra ="selected" was coming
from...
You were using an editor that creates XHTML instead of HTML.
But then, XHTML might be want you wanted in the first place.
probably asp was munging it as it went out for whatever reason.
Probably not.
Bottom line, it didn't work.
Of course it did not.
I'd never even have bothered with the javascript
And as it seems, with HTML neither.
if the asp code would have worked right off. I still have no Idea why the
expression never returned true inside a loop, while outside a loop it
would evaluate as expected.


Well, you should figure out where nPages comes from.
PointedEars
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Albrecht | last post by:
Hi folks, Scenario: A form on a web page that passes values to a dll. We want the user to be able to choose among several locations. The value of the variable that gets passed is a 3-letter code...
7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
5
by: callmebill | last post by:
I'm relatively new to javascript, and I'm trying to decide whether the following (and if so, clues on how to do it): I'd like to create two HTML multiple-select boxes. The first would be a list...
4
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
5
by: chaos | last post by:
Hi , I stuck in my project as i face a problem How to show the available time of the venue when i select the day?? Need to connect to the database to find out the available time of the venue on...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
2
by: Sudhakar | last post by:
i have two select tags as part of a registration form, city1 city2 where city1 has a list of regions and similar for city2 there are different regions for city1 and city2 so instead of all the...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.