1

I'm using this jQuery multiselect plugin in a form and no idea how to validate whether any option is selected from it. https://github.com/nobleclem/jQuery-MultiSelect

This is how I tried to do it but it doesn't work.

HTML

<select multiple id="selectbox">
    <option value="1"></option>
    <option value="2"></option>
    <option value="3"></option>
</select>
<input type="button" value="validate" id="btn"/>

JS

$(document).ready(function () {
    $('select[multiple]').multiselect();

    $('#btn').click(function(){
        if ($("#selectbox option:selected").length == 0) {
            alert('None Selected');
        }
    });
});
3
  • Your last line wants to be }). Commented Apr 18, 2018 at 10:58
  • <select mutiple … -> <select multiple … Commented Apr 18, 2018 at 11:16
  • Sorry it was a typing mistake while typing in here. Commented Apr 18, 2018 at 11:23

1 Answer 1

3

there is the mistake in select box multiple. try this,

<select id="selectbox" multiple>
    <option value="1">1 option</option>
    <option value="2">2 option</option>
    <option value="3">3 option</option>
</select>

$(document).ready(function () {
    $('select[multiple]').multiselect();
    $('#btn').click(function(){
        if ($('#selectbox').val() != null && $('#selectbox').val().length > 0) {
            console.log('Selected');
        }else{
            console.log('Non Selected');
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.