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

385
Vistas
How to capture value selected from a dropdown list?

I have a simple form that I am creating using react JS. I have 2 text fields, Type & Year, and one dropdown which is car. The car dropdown is populated using a value coming from an API. The user enters text value for the first 2 fields and selects the value for car from the dropdown menu and hits add. I have another post API that captures this value. I can store and set the value for the text boxes but for the dropdown, the value of car is not getting captured and I get a null value. How can I store the value of car like I am doing for other 2 text boxes

import { useHistory } from "react-router-dom";
import axios from "axios";
import React,{useState,useEffect} from 'react';

function NewCar() {

  const [type, setType] = useState("");
  const [year, setYear] = useState("");
  const [car, setCar] = useState("");
  let history = useHistory();

  const [entries,setEntries]=useState([])
  
  useEffect(() => {
    fetchValues();
  }, [])

  useEffect(() => {
    console.log(entries)
  }, [entries])
  
  const fetchValues = async()=>{
    const response = await axios('http://localhost:8080/cars');
    setEntries(response.data)
  }

  const onSubmit = function (e) {
    e.preventDefault();
    axios
      .post("http://localhost:8080/update", {
        type,
        year,
        car
      })
      .then(() => history.push("/"));
  };

  return (
    <form onSubmit={onSubmit}>
    <table border="1">
      Type:{" "}
      <input
        name="type"
        value={type}
        onChange={(e) => setType(e.target.value)}
      />
      <br />
      Year:{" "}
      <textarea
        name="year"
        value={year}
        onChange={(e) => setYear(e.target.value)}
      ></textarea>{" "}
      <br />
      <label for="car">Car:</label>
        <select id="car" name="car">
          {entries.map((entry) => (
             <option value={entry.name}>{entry.name}</option>
          ))}
          name="car"
          value={car}
          onChange={(e) => setCar(e.target.value)}
       </select>
      <br />
    </table>
      <button>Add</button>
    </form>
  );
}

export default NewCar;


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

0

I believe you made a mistake in your code by placing your attributes outside the select tag, it should look something like this:

  <select
    id="car"
    name="car"
    value={car}
    onChange={(e) => setCar(e.target.value)}
  >
    {entries.map((entry) => (
      <option value={entry.name}>{entry.name}</option>
    ))}
  </select>

I have this sample in which u can get the the value of a select tag or dropdown, hope you can reference from it.

const [dataItem, setDataItem] = useState("");

  const data = [
    {
      name: "Ford",
      year: "1903",
    },
    {
      name: "Chevy",
      year: "1904",
    },
    {
      name: "Dodge",
      year: "1905",
    },
    {
      name: "Honda",
      year: "1906",
    },
    {
      name: "Toyota",
      year: "1907",
    },
  ];

  return (
    <>
      {dataItem}
      <select
        name="dataItem"
        value={dataItem}
        onChange={(e) => setDataItem(e.target.value)}
      >
        {data.map(({name}) => (
          <option value={name}>{name}</option>
        ))}
      </select>
    </>
  );

If it still went wrong, please let me know and write a comment below.

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