SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Select Options!

    I want to have a select box with the ablillity to select multiple items! But how then do you retrieve a selectedIndex value! Thanx

    Jon

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The selectedIndex property will always report only the first selected option, if there are multiple. You'd need to make your own function to return an array of indicies
    Code:
    function getSelectedIndicies( selElem )
    {
    	var indicies = new Array(), i = -1, opt;
    	while( opt = selElem.options[++i] )
    	{
    		if ( opt.selected ) indicies[indicies.length] = i;
    	}
    	return indicies;
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanxs Beetle I was thinking that I would have to write my own function! Thanx again!

    Jon

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Naturally, you easily could alter Beetle's snippet to return an array of the values instead, or even an array of the selected <option> elements themselves, which might be useful, depending what you're doing...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point, M@rco. It would be foolish to return an array of indicies, only to use it to access the options collection again. I often just return an array of references to the OPTION elements.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wasn't detracting from your answer - yours was a perfect match for the requirements he stated (I would expect nothing less from you!)

    It was merely that depending on what he was doing afterwards, he might in fact find the corresponding values or elements more useful...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by M@rco
    I wasn't detracting from your answer
    Of course you weren't! [img]images/smilies/biggrin.gif[/img]

    My "foolish" comment was hypothetical - I don't know how he's using them either.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then we're agreed!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  9. #9
    SitePoint Zealot Arkkimaagi's Avatar
    Join Date
    Mar 2002
    Location
    Lappeenranta, Finland
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good for you guys

  10. #10
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried to have it return an option obj but it doesnt seem to work just returns undefined!

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Show us what you tried! I can't debug code that I can't see!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surely all that's needed is to change this:
    Code:
    if ( opt.selected ) indicies[indicies.length] = i;
    to this:
    Code:
    if ( opt.selected ) indicies[indicies.length] = opt;
    (untested)
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  13. #13
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's all that is needed. Although, I'd personally change the function name to fit the context of usage
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  14. #14
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was able to get it working sorry I ment to post the code just forgot to!

  15. #15
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A much more faster approach is listed here ....
    How to get selected items from <select multiple ... > using Javascript

    Hope this helps!

  16. #16
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see the performance difference, but is it desirable to deselect the options chosen? Do you re-select them in your actual application?

    Also - a global variable for results? boo

    Now, for the application vs. theory argument...

    A multiple select with 1000 options? Sure, it's possible, and your function will be much faster, but an application that even has such a widget is poorly designed in the first place, IMO. A multiple select should never be larger than what a user can mentally handle. Theory is fantastic, but actual application should always be included in the design process.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  17. #17
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actual, the code is bit modified to suit my needs which I used in my application. Here it is listed for reference, you can modify it as you like ....
    If you don't wanna use the global variable, fine don't use it. I am not insisting it ... (Besides, it was meant to show the example listed there ...)

    And YEAH my application did require a couple of hundred selects (if not 1000). I am writing this from experience, not just some random blabbar ....

    P.S.: If you wanna see the it actual use, try http://www.diamondpublication.com/bookseller/
    With
    user: bachan
    pw: :bachan


Bookmarks

Posting Permissions

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