www.webdeveloper.com
Results 1 to 7 of 7

Thread: Auto selecting an option in selection box

  1. #1
    Join Date
    Aug 2006
    Posts
    162

    Auto selecting an option in selection box

    Hi

    I would like to know is possible to have javascript auto select a option or two in a multiple selection box and then submit it.

    Meanning that I have a selection box and I want javascript to select 2 or more options in the selection box by it self and the submit it.

    If possible how will this be done?


  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    yes, but i would like to know which will be the event (the action of the user): onclick? On what? And how to youinput the values (the indexes of the options)? Are there static (preset) ot are to be input dynamically by the user in a way or another?

    Can you detail a little bit?

  3. #3
    Join Date
    Aug 2006
    Posts
    162
    Ok,

    What I am doing is as follow:

    I have two selection boxes "A" and "B".
    "A" is populated by a DB.
    Now the user wil select a option (Name of a person) in "A".
    The Second selection box "B" is then populated by the selections made in "A"
    Now the user has selected the team (From "A" to "B") and wants to submit it.

    On the form there is a buttun with a onClick event, this will then call the function thaat will select all the options in "B" and submit it.

    Hope this awnsers the q.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function selectAll(f){
    var op=f['B'].options;
    for(var i=0;i<op.length;i++){
    op[i].setAttribute('selected',true)
    }
    }
    </script>
    </head>
    <body>
    <form action="" onsubmit="return selectAll(this)">
    <select name="B" multiple>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    </select>
    <input name="sub" type="submit" value="GO">
    </form>
    </body>
    </html>

  5. #5
    Join Date
    Aug 2006
    Posts
    162
    Thanks Kor this realy helps

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by gogelpot
    Thanks Kor this realy helps
    Take care, I have edited my post:

    op[i].setAttribute('selected','selected')
    (as initially I have written) will not work in Mozilla, so that I have corrected, and the crossbrowser correct syntax is:

    op[i].setAttribute('selected',true)

  7. #7
    Join Date
    Aug 2006
    Posts
    162
    Ok Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles