Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
putting message in dropdownlist in react

I am calling an API and output of that API I am putting on the dropdown list.

enter image description here

These are the country code in my drop down list. I want to put default message in drop down like. "Please select the country code" once user click then he can select countrycode. here by default EF is selected. in place of EF I want to put message.

Below output I am putting on the list.

 componentDidMount() {
    axios.get(`http://localhost:8080/country_code`).then((res) => {
      const countryData = res.data;
      this.setState({ countryData });
      alert(countryData);
    });
  }

dropdown code

 <select
            name="countriesValue"
            value={this.state.countriesValue}
            onChange={this.selectCountryCode}
          >
            {this.state.countryData.map((countryData) => (
              <option key={countryData} value={countryData}>
                {countryData}
              </option>
            ))}
          </select>

method:-

  selectCountryCode(e) {
    e.preventDefault();
    this.setState({ countriesValue: e.target.value });
  }

state variable

this.state = {
      countryCode: '',
      countryData: [],
      countriesValue: '',
    };

what mistake I am doing in my select

Edit1:-

<select
            name="countriesValue"
            value={this.state.countriesValue}
            onChange={this.selectCountryCode}
          >
            <option value="" selected disabled hidden>
              Please select the country code
            </option>
            {this.state.countryData.map((countryData) => (
              <option key={countryData} value={countryData}>
                {countryData}
              </option>
            ))}

          </select>

After including this line

<option value="" selected disabled hidden>
              Please select the country code </option>

once user select the dropdown this Please select the country code does not appear again

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To add a default value to the <select> element, you can add a disabled and hidden option like the following:

<select
 name="countriesValue"
 value={this.state.countriesValue}
 onChange={this.selectCountryCode}
>
 <option value="" selected disabled hidden>Please select the country code</option>
 {this.state.countryData.map((countryData) => (
   <option key={countryData} value={countryData}>
     {countryData}
   </option>
 ))}
</select>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda