Shan
Shan

Reputation: 1138

How to not show a drop down with 'select2' if it has only one choice?

We want to use select2 in a way such that if we pass only one choice for the dropdown there should be no dropdown. Just the one option selected by default. Is this possible to do ?

Upvotes: 0

Views: 90

Answers (1)

Seunhaab
Seunhaab

Reputation: 540

If it has to be JS on the clienside:

HTML:

<body onload="checkSelectBox()">

JS:

var select = document.getElementById("idOfYourSelect");
if (select.length == 1)
{
    document.getElementById("outerDiv").innerHTML = select.options[0].text; 
    select.parentNode.removeChild(select);
}

This assumes there's a div around it in which you want the text of the option, since you did not bother to share any of your code I can only guess.

Or you could simply disable the select.

select.setAttribute("disabled", true);

Upvotes: 1

Related Questions