selecting an option from a select

Discussion in 'Javascript' started by SGershon, May 2, 2005.

  1. SGershon

    SGershon Guest

    Hi!

    Since the first questions I asked here, I continue to drop by and read
    some topics here and there, I like the replies very much.

    I have today another question, I don't seem to find it on the FAQ
    (please point me if its there):

    I have two selects, one written 'directly on the page' and a second
    written dinamically by JavaScript. (Actually, both are written by php
    using data from a DB).

    One is dependable of the other, and I am using the code of DevArticles
    to make them depend:
    http://tinyurl.com/df6vf

    The Question: Now, I want to select the selected option of both inputs,
    after the page is loaded.
    How can I make the right option selected, based on the value of it?

    I know the id and the value of each <OPTION>, but I do not know the
    position of the <OPTION> inside the <SELECT>. So I guess I can not use
    something like document.form.selectinput.options[1].selected, unless I
    iterate through all the options. If this is the way to do it, how is
    the iteration to be done?

    I hope you can help me on this, and thank you for all the times you
    helped me on the past!

    SGershon
     
    SGershon, May 2, 2005
    #1
    1. Advertising

  2. SGershon

    SGershon Guest

    Hi again!
    Maybe someone can point me why this wont work to solve the above
    problem/question?

    function setDefault()
    {
    for (var i=0 ; i<document.theForm.theSelect.length ; i++)
    {
    if (document.theForm.theSelect.options.value == optVal)
    {
    document.theForm.theSelect.options.selected=true
    }
    }
    }

    Thanks!
    SGershon
     
    SGershon, May 3, 2005
    #2
    1. Advertising

  3. SGershon

    RobB Guest

    SGershon wrote:
    > Hi again!
    > Maybe someone can point me why this wont work to solve the above
    > problem/question?
    >
    > function setDefault()
    > {
    > for (var i=0 ; i<document.theForm.theSelect.length ; i++)
    > {
    > if (document.theForm.theSelect.options.value == optVal)
    > {
    > document.theForm.theSelect.options.selected=true
    > }
    > }
    > }
    >
    > Thanks!
    > SGershon


    Probably because: you're not calling it onload.

    function setDefault(val)
    {
    var f, s, i = 0;
    if ((f = document.theForm)
    && (s = f.elements.theSelect))
    {
    for (var l = s.options.length; i < l; i++)
    {
    if (s.options.value == val)
    {
    s.options.selected = true;
    return;
    }
    }
    }
    }

    window.onload = function()
    {
    setDefault('foo');
    }
     
    RobB, May 3, 2005
    #3
  4. SGershon

    SGershon Guest

    No, it was some stupid typo on the code on the page.

    Thanks for the hint, RobB... I am calling it on the end of the page,
    inside a <script></script> tag... I guess is more clean/elegant to use
    the onLoad event. What are the practical differences?
     
    SGershon, May 3, 2005
    #4
  5. SGershon

    RobB Guest

    SGershon wrote:
    > No, it was some stupid typo on the code on the page.
    >
    > Thanks for the hint, RobB... I am calling it on the end of the page,
    > inside a <script></script> tag... I guess is more clean/elegant to

    use
    > the onLoad event. What are the practical differences?


    Helps you keep your 'behavioral layer' separate from that other
    rubbish:

    http://www.alistapart.com/articles/scripttriggers/

    Also, manipulating elements onload gives them an extra bit of time to
    'set up', the absence of which is sometimes a source of some
    verdrie├člichkeit, if you know what I mean. Just remember,
    window.onload is an object property & will be over-written if you
    assign to it more than once...unless, of course, you use W3C DOM
    addEventListener() or MSIE attachEvent(), or 'bundle' multiple handlers
    in a wrapper function.
     
    RobB, May 3, 2005
    #5
  6. SGershon

    RobG Guest

    SGershon wrote:
    > No, it was some stupid typo on the code on the page.
    >
    > Thanks for the hint, RobB... I am calling it on the end of the page,
    > inside a <script></script> tag... I guess is more clean/elegant to use
    > the onLoad event. What are the practical differences?
    >


    The other side of the coin is that using onload with a heavy page can
    cause an unacceptable lag in a script executing where visible page
    elements are being modified.

    Say you have something small to do a the top of the page that you'd
    prefer happened without being noticed. Inserting a function just
    after the element concerned can allow it to happen much sooner (and
    less perceptibly) than if the browser waits for onload.

    But that would be the exception rather than the rule.

    --
    Rob
     
    RobG, May 5, 2005
    #6
    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. Robin
    Replies:
    1
    Views:
    1,026
    =?Utf-8?B?R3JlZyBIdXJsbWFu?=
    Aug 6, 2004
  2. J. Hall
    Replies:
    2
    Views:
    1,073
    Jeff Thies
    Apr 21, 2004
  3. WebRaster
    Replies:
    1
    Views:
    270
    Francisco Spaeth
    Oct 26, 2005
  4. Nick Spacek

    Removing select option removes entire select

    Nick Spacek, Jun 21, 2006, in forum: Javascript
    Replies:
    1
    Views:
    151
    Matt Kruse
    Jun 21, 2006
  5. rvelosoo
    Replies:
    1
    Views:
    339
    Bart Van der Donck
    Aug 13, 2008
Loading...

Share This Page