views:

427

answers:

4

I'm using a select tag in a form i'm making that allows multiple selections, but i want to make the maximum amount of selections 10. is this possible using javascript or jquery?

Thanks in advance!

+2  A: 

Using jQuery you can attach a function to the change event and since it is a multiple select the val() will be an array. You can always check the length of the array and do something if its a predefined size. The following code demonstrates how you will know how many items are selected.

  $("#select").change(function(){
        alert($(this).val().length);
  });   
Vincent Ramdhanie
that will help a lot. is there a way to deselect evrything in a select box, also?
tominated
@tominated - Using jQuery, you can simply call `$('#myselectbox_id').val(null);`
Topher Fangio
You can use this: `$("#select").val([]);` to set no options in the select list.
Vincent Ramdhanie
or as @Topher suggested.
Vincent Ramdhanie
+1  A: 

Here is some full code for you to use...gotta love the Google AJAX API Playground :-)

Edit 1: Note: this only lets you choose 5 because I didn't feel like copy/pasting another 10 options :-)

<!--
  copyright (c) 2009 Google inc.

  You are free to copy and use this sample.
  License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Sample Select Maximum with jQuery</title>
    <script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"&gt;    </script>
    <script type="text/javascript">
    google.load("jquery", "1");

    $(document).ready(function() {

      var last_valid_selection = null;

      $('#testbox').change(function(event) {
        if ($(this).val().length > 5) {
          alert('You can only choose 5!');
          $(this).val(last_valid_selection);
        } else {
          last_valid_selection = $(this).val();
        }
      });
    });
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <select multiple id='testbox'>
      <option value='1'>First Option</option>
      <option value='2'>Second Option</option>
      <option value='3'>Third Option</option>
      <option value='4'>Fourth Option</option>
      <option value='5'>Fifth Option</option>
      <option value='6'>Sixth Option</option>
      <option value='7'>Seventh Option</option>
      <option value='8'>Eighth Option</option>
      <option value='9'>Ninth Option</option>
      <option value='10'>Tenth Option</option>
    </select>
  </body>
</html>

Topher Fangio
that's almost perfect! it doesn't select it again if last_valid_selection is an array. Thanks, though!
tominated
I'm not sure what you mean. What doesn't select it again if `last_valid_selection` is an array?
Topher Fangio
You may want to move `last_valid_selection` out of the global scope and into the `ready` anonymous function.
Justin Johnson
@Justin Johnson - Good point. I'm still confused as to what doesn't work though. Works perfectly in Chrome (can't test IE at the moment).
Topher Fangio
Works fine in FF 3.5.6 as well. I don't know what the down vote is for
Justin Johnson
it's not working with the code i have so far. i have a comment on the op with soem more info on what i'm doing
tominated
@tominated - You downvoted me because the example I gave you doesn't work for your exact situation?
Topher Fangio
@Topher, don't answer unless you're prepared to read minds! :)
Jonathan Sampson
@Jonathan +1 - LMAO. Thanks, I'm about to head to bed and I needed a laugh :-)
Topher Fangio
+1  A: 

This would limit the user to 3 options:

$("#mySelect option").click(function(e){
  if ($(this).parent().val().length > 3) {
    $(this).removeAttr("selected");
  }
});
Jonathan Sampson
A: 

figured it out! here's the resulting code:

$(document).ready(function(){

var last_valid_selection = null;
var selected = "";

$("#recipient_userid").change(function(event){

    if ($(this).val().length > 10) {

        alert('You can only choose 10!');
        $(this).val(last_valid_selection);

    } else {

        last_valid_selection = $("#recipient_userid").val();

        $("#recipient_userid option:selected").each(function () {
            selected += "<li>" + $(this).text() + "</li>";
        });

        $("#currentlySelected").html(selected);
        selected = "";
    }

}).change();

});

Thanks for all your help guys!

tominated
Tominated, this is not a forum. Your contributes ought only exist in the original question (you can edit it as much as you like). You should accept one of the many fine answers that were provided here.
Jonathan Sampson
Tominated, Jonathan is absolutely correct. The idea is to ask a question and select the best answer (you even get a few reputation for doing so). Very rarely do I not select an answer and even more rare is it that I answer my own question. However, this community is here to help each other, and I'm glad we helped you.
Topher Fangio
Sorry guys, I was unaware about that.
tominated