Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!