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

206
Vistas
How to query a number value from a drop-down menu in JavaScript?

My HTML

  <form id="form">
        <div id="tshirtOrder">
            <button id="tshirt">T-Shirt</button>
            <label>Quantity:</label>
            <select name="tshirtQuantity" required>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>    
            </select>
        </div>

My JS

const getForm = () => {
    const formEl = document.getElementById('form')
    let tshirtQuantity = formEl.elements.tshirtQuantity.value    
}

I am trying to sell t-shirt item with the quantity option (number value) as a drop-down list. My question is how can write a syntax to return the quantity given in number of the value 1, 2, 3? The JavaScript I wrote always return the first option value (1) inside of <select name="tShirtQuantity">. I want to query the correct number value that the user selected from the drop-down menu. Thanks!

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

0

You can use querySelectorAll, spread operator and map function as

let selectElement = document.querySelectorAll('[name=tshirtQuantity]');
let optionValues = [...selectElement[0].options].map(o => parseInt(o.value)) 

If you only want to get selected value

console.log(selectElement[0].options[selectElement[0].selectedIndex].value)

const getForm = () => {
    let selectElement = document.querySelectorAll('[name=tshirtQuantity]');
    let optionValues = [...selectElement[0].options].map(o => parseInt(o.value));
    console.log(optionValues);
    
    console.log(selectElement[0].options[selectElement[0].selectedIndex].value)
}

getForm();
<form id="form">
        <div id="tshirtOrder">
            <button id="tshirt">T-Shirt</button>
            <label>Quantity:</label>
            <select name="tshirtQuantity" required>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>    
            </select>
        </div>
 </form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The following method is old school solution.

Replace:

let tshirtQuantity = formEl.elements.tshirtQuantity.value     

with:

let tshirtQuantity = formEl.elements.tshirtQuantity.options[formEl.elements.tshirtQuantity.selectedIndex]
about 4 years ago · Juan Pablo Isaza Denunciar

0

There are a few problems with your code.

First, the HTML. You are closing the label in the wrong place, and the HTML element you should listen to is not the form, but the select.

<form id="form">
   <div id="tshirtOrder">
      <label>Quantity:
         <select class="tshirt" name="tshirtQuantity" required>
             <option value="1">1</option>
             <option value="2">2</option>
             <option value="3">3</option>    
         </select>
      </label>
    </div> 
</form>

Then, to get the change on the element, you should add an event listener that reacts to the change.

const selectElement = document.querySelector('.tshirt');

selectElement.addEventListener('change', (event) => {
    console.log(event.target.value)
});

You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event

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