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

248
Vistas
get a data-attribute (data-phone) value from the selected datalist option

how to get a data-attribute (like:- data-phone ) value from the selected datalist option which is selected; It send me just value not data attribute.

i want to set data-phone of input from data-phone of datalist > option

How can I set this? Help

Html:

     <input type="text" name="patirnt_name" id="patirnt_name" list="patient_list" onfocus="getAllPatients()" value="" data-phone="">
         <datalist id="patient_list">

         </datalist>

JQuery:

function getAllPatients() {
            let url = "{{ route('get-all-patient-ajax') }}";
            let list = $('#patient_list');

            let html = '';
            $.ajax({
                url: url,
                type: "GET",
                success: function(res) {
                    let data = res.patients;

                    $.each(data, function(index, value) {
                        html +=
                            `<option value="${value.F_name}" data-phone="${value.phone}" >${value.F_name}</option >`;
                    });

                    list.html(html);

                },
                error: function(error) {
                    console.log(error);
                }
            })
        }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As far as I understand, when you list.html(html) all the options are displayed in the DOM without the data-phone="${value.phone}" that you set in the each loop. You could try this:

$.each(data, function(index, value) {
    $('<option/>', { 
          value : value.F_name,
          data-phone : value.phone
    }).text(value.F_name).appendTo(list);
});
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