Select Option & selected

Discussion in 'Javascript' started by brian.ackermann, Apr 6, 2005.

  1. 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
     
    brian.ackermann, Apr 6, 2005
    #1
    1. Advertising

  2. brian.ackermann

    Erwin Moller Guest

    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
     
    Erwin Moller, Apr 6, 2005
    #2
    1. Advertising

  3. 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 :)
     
    brian.ackermann, Apr 6, 2005
    #3
  4. brian.ackermann

    RobB Guest

    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...
     
    RobB, Apr 6, 2005
    #4
  5. 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
     
    brian.ackermann, Apr 6, 2005
    #5
  6. brian.ackermann

    RobB Guest

    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 = 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.
     
    RobB, Apr 6, 2005
    #6
  7. 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
     
    brian.ackermann, Apr 6, 2005
    #7
  8. 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
     
    Thomas 'PointedEars' Lahn, Apr 11, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Olurotimi Ige
    Replies:
    6
    Views:
    523
    =?Utf-8?B?TmljZW1hbg==?=
    Mar 11, 2005
  2. J. Hall
    Replies:
    2
    Views:
    1,074
    Jeff Thies
    Apr 21, 2004
  3. Dennis Marks
    Replies:
    1
    Views:
    3,396
    Nikita the Spider
    Sep 8, 2006
  4. John
    Replies:
    5
    Views:
    999
    dorayme
    Mar 1, 2010
  5. Mark Kolber
    Replies:
    4
    Views:
    348
Loading...

Share This Page