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

477
Vistas
Getting selected datalist option value, and innerHTML, without using jQuery

I have a situation where I want to let a user decide titles of books that I have on my db, using an input with a datalist (generated by php), after the user picked a title, he would click a submit button and the form would send the title in another file.

Everything worked fine but I didn't realized that I needed to send the ID of the book that the user selected, because there can be more than one book with the same title.

What I would like to have is the option of the datalist, that no longer has the title of the book inside its "value" attribute, but I want that title inside its innerHTML, so that the title gets displayed, while having the ID inside the "value" attribute. My problem is that if I do that, when the user clicks on the datalist option, the ID gets inside the text input, so the user may not know what book he choose.

summing up: I would like to have the datalist that displays the title, when an option is chosen, that title gets displayed in the text input, when I submit, the Id of the book gets sent in "FindBook.php" inside $_POST.

isIn() checks if the title is inside the array of titles, I would need to change that so that it can check if the ID is inside the array of IDs.

<form onsubmit="alert(document.getElementById('number').value);" action="FindBook.php" target="_blank" method="POST">
      <input id="number" list="BooksById">
      <input type="submit" value="Find">
    </form>
    
<datalist id="BooksById">
      <option value="1">Title1</option>
      <option value="2">Title2</option>
      <option value="3">Title3</option>
      <option value="4">Title4</option>
  </datalist>
  
<br>

<form onsubmit="alert(document.getElementById('string').value);" action="FindBook.php" target="_blank" method="POST">
      <input id="string" list="booksByTitle">
      <input type="submit" value="Find">
    </form>
    
<datalist id="booksByTitle">
      <option value="Title1"></option>
      <option value="Title2"></option>
      <option value="Title3"></option>
      <option value="Title4"></option>
  </datalist>

Since I don't understand jQuery I would really prefer a solution that doesn't imply that.

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

0

I think that in your case you must use a <select> tag instead of a <datalist> because you do not want the end user to enter new names (or yes?). However you can work with the data attributes like in the code below:

HTML:

<input list="titles" id="title-input" oninput="getBookId()">
<datalist id="titles">
  <option data-value="5" value="A book name">
</datalist>

JavaScript:

function getBookId() {
  var selectedTitle = document.getElementById("title-input").value;
  var value2send = document.querySelector(`#titles option[value='${selectedTitle}']`).dataset.value; // Here now you have the book title id.
  console.log("getBookId ~ value2send", value2send)
}

I hope it works for you.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Assuming you can provide distinct title values for each datalist option...

Add a dataset attribute, such as data-id, to your datalist option elements, containing the corresponding id, and add a hidden type input to your form. Then use the onsubmit event handler function to get the selected datalist option's dataset id value and assign it to the value of the hidden input:

function findBook(form) {
  form.bookid.value = document.querySelector(`datalist option[value="${form.booktitle.value}"]`).dataset.id;
  console.log(form.bookid.value);
  return false;
}
<form onsubmit="return findBook(this)">
  <input type="hidden" name="bookid">
  <input name="booktitle" list="BooksById">
  <input type="submit" value="Find">
</form>

<datalist id="BooksById">
  <option data-id="1" value="Title1">
  <option data-id="2" value="Title2">
  <option data-id="3" value="Title3">
  <option data-id="4" value="Title4">
</datalist>

Upon form submission, your PHP file will have the variables $_POST['bookid'] and $_POST['booktitle'] available.

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