Connecting Tech Pros Worldwide Forums | Help | Site Map

Select Option & selected

brian.ackermann
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Erwin Moller
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Select Option & selected


brian.ackermann wrote:

Hi Brian,
[color=blue]
> 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" >[/color]

Try to avoid that ancient bad script-tag
Use:
<script type="text/javascript">
[color=blue]
> 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?[/color]

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
[color=blue]
>
> Thanks,
>
> Brian[/color]

brian.ackermann
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Select Option & selected


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 :)

RobB
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Select Option & selected


brian.ackermann wrote:[color=blue]
> 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[/color]
is[color=blue]
> working, on firefox.
>
> Grr.
>
> Thanks though :)[/color]

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...

brian.ackermann
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Select Option & selected


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

RobB
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Select Option & selected



brian.ackermann wrote:[color=blue]
> True, that, its just there because I WAS document.write(ing) the[/color]
first[color=blue]
> couple ways I tried to fix this.
>
> This definately gets me closer. In testing, I found that applying[/color]
your[color=blue]
> changes (RobB) that firefox continues to behave as I desire, and now[/color]
IE[color=blue]
> 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[/color]
ideas?[color=blue]
>
> Brian[/color]

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.

brian.ackermann
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Select Option & selected


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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Select Option & selected


brian.ackermann wrote:
[color=blue]
> 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".[/color]

Because it is invalid. You might want

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

Check out <http://validator.w3.org/>!
[color=blue]
> I tried figuring out where that extra ="selected" was coming
> from...[/color]

You were using an editor that creates XHTML instead of HTML.
But then, XHTML might be want you wanted in the first place.
[color=blue]
> probably asp was munging it as it went out for whatever reason.[/color]

Probably not.
[color=blue]
> Bottom line, it didn't work.[/color]

Of course it did not.
[color=blue]
> I'd never even have bothered with the javascript[/color]

And as it seems, with HTML neither.
[color=blue]
> 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.[/color]

Well, you should figure out where nPages comes from.


PointedEars
Closed Thread