Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set limit in multiple selection dropdown list?

I am using a multiple selection drop-down list in my site.This is working properly and we can select more than one options from that list.But I want select only 3 options from that list.Is it possible to set limit for that??

I am using the code from http://www.aleixcortadellas.com/main/2009/03/20/492/

like image 532
Natasha Avatar asked Dec 28 '22 01:12

Natasha


1 Answers

use jQuery and following click function, it will help

$(document).ready(function() 
{
    $('#slectboxid option').click(function() 
    {
        var items = $(this).parent().val();
        if (items.length > 3) {
                       alert("You can only select 3 values at a time");
           $(this).removeAttr("selected");
        }
    });
   });

Edit: use .prop() instead of .removeAttr(),.removeAttr() is deprecated in newest jQuery libraries

like image 125
arun Avatar answered Jan 13 '23 16:01

arun