Count selected elements of a select box with javascript

0

Greetings, I have a select box (id_encuentros) and I need to know how many elements have been selected to restrict it, that is, if you have selected more than one option, then I would show you an alert message.

Does anyone know how to do it in javascript or jquery?

Thank you.

    
asked by FangusK 20.02.2017 в 18:17
source

2 answers

1

In the end I found the solution, in case someone is worth it:

<script>
function myFunction() {
    var count = $("#id_acciones :selected").length;
    if(count>1) {
        alert("Solo puede seleccionar un curso")
    }
}
</script>

Thanks for your help!

    
answered by 20.02.2017 / 18:46
source
0

It would be a bit of code on your part to put something more concrete, but with jQuery you can do something like this:

$(':checked').size()

Be sure to restrict the elements by input, by name, by some type of attribute, etc.

Reference: link

    
answered by 20.02.2017 в 18:36