1
<select open={false} autoFocus={true} onClick={this.handleClick} size={5}>
  <option value="grapefruit">Grapefruit</option>
  <option value="lime">Lime</option>
  <option value="coconut">Coconut</option>
  <option value="mango">Mango</option>
</select>

I have made a dropdown, but I cannot close it. I do not want close it with display:none

1
  • You have set the size to 5. So every time dropdown list shows 5 items. If you want to close it after selecting one, you can dynamically set it to 1. Commented Dec 28, 2021 at 14:58

2 Answers 2

1

You can initially show 5 items and, after selecting close it(set to 1 using a state)

import React, { useState } from "react";

function App() {
  const [defaultSize, setDefaultSize] = useState(5);

  const handleClick = () => {
    setDefaultSize(1);

    // do other stuff
  }

  return (
    <div>
      <select open={false} autoFocus={true} onClick={handleClick} size={defaultSize}>
        <option value="grapefruit">Grapefruit</option>
        <option value="lime">Lime</option>
        <option value="coconut">Coconut</option>
        <option value="mango">Mango</option>
      </select>
    </div>
  );
}

export default App;
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the size attribute. change onClick to onChange add value attribute for select tag

 <select value={myCar} onChange={handleChange}>

1 Comment

i use size attribute to open drop down default. i am looking for solution with size attribute

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.