S. S. Rawat
S. S. Rawat

Reputation: 6111

Check whether an item from a dropdown list is selected

I have a multi-select HTML dropdown list. I want to check on button click whether there is any item selected or not. If there is no element selected, then alert "Item not selected" else for selected items alert "selected".

if ($("#ddl1 >option").length >= 1) {

    if ($("#ddl1 >option:selected").val() == 'undefined') {
        alert("Not selected"); 
    } else {
        alert("deleted"); 
    }

} else
    alert("list empty");

Upvotes: 2

Views: 3168

Answers (2)

S. S. Rawat
S. S. Rawat

Reputation: 6111

Here i have a list in which three item and check value of selected dropdown

  if ($("#ddl1 >option").length >= 1) {

        if ($("#ddl1").val() <0) {
            alert("Not selected"); 
        } else {
            alert("deleted"); 
        }

    } else
        alert("list empty");

Upvotes: 0

Somnath Kharat
Somnath Kharat

Reputation: 3610

You can use length <= 0 here

if ($("#ddl1 > option").length >= 1) {
    if ($("#ddl1 > option:selected").length <= 0) {
        alert("Not selected");
    } else {
        alert("deleted");
    }
} else alert("list empty");

Upvotes: 2

Related Questions