Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onload option(x)selected

    how can I select an option onload
    without the onload into body tag ?

    <head>
    <script type="text/javascript">
    function select()
    {
    document.onload = document.forms[0].myList.options[2].selected
    }
    </script>
    </head>
    <body>
    <select name="myList">
    <option value="0">zero</option>
    <option value="1">one</option>
    <option value="2">two</option>
    </select>
    </body>

    does'nt work

    thank you

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts

    Re: onload option(x)selected

    document.onload = document.forms[0].myList.options[2].selected = true;
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    doesn't work

    <script type='text/javascript'>
    function selection()
    {
    document.onload = document.forms[0].myList.options[40].selected =true;
    }
    </script>

    I don't know why it doesn't work !

    nothing is selected

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code

    the complete code

    -------------------------------------

    <html >
    <head>
    <script type='text/javascript'>
    function selection()
    {
    document.onload = document.forms[0].myList.options[2].selected =true;
    }
    </script>
    </head>
    <body>
    <select name='myList' id='myList'>
    <option value='0'>zero</option>
    <option value='1'>one</option>
    <option value='2'>two</option>
    </select>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FORM

    yes of course FORM tags are missing . but nothing change it doesn't work

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts

    Re: code

    Code:
    <html >
    <head>
    <script type='text/javascript'> 
    document.onload = document.forms[0].myList.options[2].selected =true; 
    </script>
    </head>
    <body>
    <select name='myList' id='myList'>
    <option value='0'>zero</option>
    <option value='1'>one</option>
    <option value='2'>two</option>
    </select>
    </body>
    </html>
    I am the luckiest man in the world

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error

    document.forms[0].myList has null value or is not an object


    it doesn'work (for IE 6)

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    then use proper form tags.

    Code:
    <html >
    <head>
    <script type='text/javascript'> 
    document.onload = document.forms[0].elements["myList"].options[2].selected =true; 
    </script>
    </head>
    <body>
    <form>
    <select name='myList' id='myList'>
    <option value='0'>zero</option>
    <option value='1'>one</option>
    <option value='2'>two</option>
    </select>
    </form>
    </body>
    </html>
    I am the luckiest man in the world

  • #9
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    no

    no !
    even getElementById ("myList") doesn't work

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Code:
    <html>
    <head>
    <script type='text/javascript'> 
    function selectoption() {
    document.forms["myForm"].elements["myListName"].options[2].selected =true;
    }
    </script>
    </head>
    
    <body onload="selectoption()">
    <form name="myForm">
    <select name="myListName" id="myListId" size="3">
    <option value='0'>zero</option>
    <option value='1'>one</option>
    <option value='2'>two</option>
    </select>
    </form>
    </body>
    
    </html>
    I am the luckiest man in the world

  • #11
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks but

    thank you for helping
    but I am looking for a solution without
    <body onload=">

    and if I take your exemple

    <script type='text/javascript'>
    document.onload = document.forms["myForm"].elements["myListName"].options[2].selected =true;

    </script>

    doesn't work

  • #12
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    not index but Value

    I want to select a value (no ides of index number !)

    how can I do it ?

    <html>
    <head>
    <script type='text/javascript'>
    function selectOption() {
    document.getElementById("myListName").options("58").selected =true;
    }
    </script>
    </head>

    <body onload="selectOption()">
    <select name="myListName" id="myListId" size="3">
    <option value='0'>rttr</option>
    <option value='11'>hhjhj jjj</option>
    <option value='58'>qq</option>
    <option value='7'>ttuk gkh</option>
    </select>
    </body>
    </html>

  • #13
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    another try

    I want to select
    the value =''58


    <html>
    <head>
    <script type='text/javascript'>
    function selectOption(num)
    {
    var liste;
    var option;

    liste = document.getElementById("myList");

    for(i=liste.length-1; i>=0; i++)
    {
    if (liste.options[i]== num)
    {
    option = i;
    }
    }
    liste.selectedIndex = option;
    }
    </script>
    </head>

    <body onload="selectOption('58')">
    <select name="myList" id="myListId" size="10">
    <option value='0'>zero</option>
    <option value='11'>onze</option>
    <option value='58'>cinquante huit</option>
    <option value='7'>sept</option>
    </select>
    </body>
    </html>


    doesn't work too

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about this?

    window.onload = function() {selectOption(59);};

  • #15
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ok but

    I have it in this way

    <html>
    <head>
    <script type='text/javascript'>
    function selectOption(liste,num) {
    document.getElementById(liste).value =num;
    }
    </script>
    </head>
    <body onload="selectOption('myListName','11')">
    <select name="myListName" id="myListId" size="10">
    <option value='0'>tuzuk</option>
    <option value='11'>onze</option>
    <option value='58'>qq</option>
    <option value='7'>q hhh</option>
    </select>
    </body>
    </html>


    I don't know how to avoid the body onload

    thank you


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •