Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

<Select> first value disabled and selected

i’m building an e-commerce site and i have a size, mapped with mongoDB, that has values ["XS","S","M","L","XL"].

The problem i got is when the user choose a product, he might see the selected size value on XS by default because it’s the first value on the object list, he wont change it and press on checkout button. Then in the cart page he will see there’s no size selected.

How to add a first value as selected and disabled so the user will be obligated to change the value of select and choose a value, just something like this in HTML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<select>
   <option selected="true" disabled="disabled">Choose a size:</option>
   <option>XS</option>
   <option>S</option>
   <option>M</option>
   <option>L</option>
   <option>XL</option>
<select/>

My code:

const FilterSize = styled.select`
  margin-left: 10px;
  padding: 5px;
  cursor: pointer;
  outline: 0px;
  font-size: inherit;
`;
//...
              <Filter>
                <FilterTitle>Size</FilterTitle>
                <FilterSize onChange={(e) => setSize(e.target.value)}>
                  {product?.product?.size.map((s) => (
                    <FilterSizeOption key={s}>{s} </FilterSizeOption>
                  ))}
                </FilterSize>
              </Filter>

>Solution :

You need to change the JSX to this

 <Filter>
    <FilterTitle>Size</FilterTitle>
       <FilterSize onChange={(e) => setSize(e.target.value)}>
        <FilterSizeOption selected={true} disabled="disabled" >Choose a size:</FilterSizeOption>
           {product?.product?.size.map((s) => (
             <FilterSizeOption key={s} value={s}>{s} </FilterSizeOption>
            ))}
       </FilterSize>
 </Filter>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading