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

175
Vistas
Pass the text from a form Select-Option to a text input field

I would like to pass the text portion (not the value) of a form Select-Option to a hidden text-input field within the same form when the user makes a selection.

I have explored some java and PHP 'examples' I found in my research, but none of them seem to work for me.

I have posted a raw example of the form to see if anyone can lead me to water. Any help wouold be appreciated.

HMTL

<html>
<head>
</head>    
<body>

<form action="fruitBasket.php" method="post" enctype="multipart/form-data">

<select id="fruitSelector"  name="fruitSelector">

<option value="0" selected="selected" disabled="disabled">Select Fruit</option>
<option value="1">Grapes</option>
<option value="2">Strawberries</option>
<option value="3">Peaches</option>
<option value="4">Blueberries</option>

</select>    

<br>
<br>

<input type="text" class="hiddenField" name="hiddenField" placeholder="Selected fruit appears 
here.">

</form>

</body>    

</html>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It's not as easy as getting the selected option's value, which can be retrieved simply as selectElement.value, yet it's not difficult at all.

selectElement.options will give you an array of ALL the options inside the select element. You will find the selected option's index to be selectElement.selectedIndex.

With that said, you can access to the selected option like this: selectElement.options[selectElement.selectedIndex].

Finally, you can get the text property like this: selectElement.options[selectElement.selectedIndex].text

Here's the code:

// THIS CONSTANT REPRESENTS THE <select> ELEMENT
const theSelect = document.getElementById('fruitSelector')

// THIS LINE BINDS THE input EVENT TO THE ABOVE select ELEMENT
// IT WILL BE EXECUTED EVERYTIME THE USER SELECTS AN OPTION
theSelect.addEventListener('input', function() {

  // THIS IS HOW YOU GET THE SELECTED OPTION'S TEXT
  let selectedOptText = theSelect.options[theSelect.selectedIndex].text
  
  // FINALLY, THIS COPIES THE ABOVE TEXT TO THE INPUT ELEMENT:
  document.querySelector('.hiddenField').value = selectedOptText;

})
<form action="fruitBasket.php" method="post" enctype="multipart/form-data">

  <select id="fruitSelector" name="fruitSelector">
    <option value="0" selected="selected" disabled="disabled">Select Fruit</option>
    <option value="1">Grapes</option>
    <option value="2">Strawberries</option>
    <option value="3">Peaches</option>
    <option value="4">Blueberries</option>
  </select>

  <br>
  <br>

  <input type="text" class="hiddenField" name="hiddenField" placeholder="Selected fruit appears 
here.">

</form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

well if you want to pass the text portion you should add the names as the values too like

---
code
---
<option value="Grapes">Grapes</option>
<option value="Strawberries">Strawberries</option>
---
code
---
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