Select Option & selected

  • Thread starter brian.ackermann
  • Start date
B

brian.ackermann

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
 
E

Erwin Moller

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

brian.ackermann

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

RobB

brian.ackermann said:
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...
 
B

brian.ackermann

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
 
R

RobB

brian.ackermann said:
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 = 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.
 
B

brian.ackermann

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
 
T

Thomas 'PointedEars' Lahn

brian.ackermann said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top