0

I have a select control in my php web site i need to show it as a drop down list collapsed and i need to select multiple option, i use the folowing code

echo '<select class="form-control" multiple name="associate" >';
echo "<option value='0' class='form-control' >---Choose Associate---</option>"; 
foreach ($userarray as $key => $value)
{
    if (isset($_REQUEST['associate']))
    {
        echo "<option value='".$key."' class='form-control' >".$value."</option>"; 
    }
    else
    {
        echo "<option value='".$key."' class='form-control' >".$value."</option>";
    }
}
echo '</select>';

The problem is that the drop down look like this in the picture, i need to collapse it not to show it like this .. i dont need to see all the items!! just first one and when i click the others appearsenter image description here

9
  • remove multiple from the <select> Commented Oct 24, 2020 at 21:44
  • i need to select multiple option in my drop down Commented Oct 24, 2020 at 21:45
  • Set the size to 1. Then when it's focused, dynamically change the size to more. When it loses focus, change the size back to 1. Commented Oct 24, 2020 at 21:51
  • 1
    Does this answer your question? How to create checkbox inside dropdown? Commented Oct 24, 2020 at 22:03
  • 1
    yes you are right i missed to write selected.. Commented Oct 25, 2020 at 8:40

1 Answer 1

1

Here's a sample of how you can do this:

<select multiple size="1" onfocus="show_more()" onblur="show_less()" id="my_select">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<script>
function show_more()
{
    document.getElementById('my_select').size = 3;
}
function show_less()
{
    document.getElementById('my_select').size = 1;
}
</script>

View in jsFiddle https://jsfiddle.net/qodw5xe9/

Note: This can be done using a single function which accepts the size as a parameter. I broke it out to explicitly show what's going on.

Sign up to request clarification or add additional context in comments.

1 Comment

i need to select multiple option in my drop down

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.