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

215
Views
Javascript and HTML datalist : manage single quote

I have an issue with my HTML datalist and the javascript to manage it... My purpose is to change the values of a HTML "select" from a datalist.

Here is my datalist :

<input class="verylarge" id="choix_client" name="choix_client" type="text" list="client" autofocus="" required spellcheck="false" onChange="getSite(this.value);" title="Les simples quotes sont remplacées par des espaces"><br>
<datalist id="client">
<?php
  foreach ($tabCLIENTS as $value) { 
    list($id,$nom) = explode(";",$value);
    $nom = str_replace("'", " ", $nom);
    ?>
    <option data-value="<?=$id?>" value="<?=$nom?>"> 
  <?php } ?>
</datalist>
<input type="hidden" name="idClient" id="idClient">

With the id number ($id), i want to change de values of the next Select option menu. My Javascript is activated on datalist change :

function getSite() {
  var NomClient = document.getElementById("choix_client").value;

  var idClient = document.querySelector("#client option[value='" + NomClient + "']").dataset.value;

  document.getElementById('idClient').value = idClient;

  $.ajax({
    type: "POST",
    url: "getSite.php",
    data: 'client=' + idClient,
    success: function(data) {
      $("#choix_site").html(data);
    }
  });
}

In my datalist, I have some Client names with single quote... (for examble : "L'EQUIPE"). It's a problem when i try to get the dataset value :

document.querySelector("#client option[value='"+NomClient+"']").dataset.value;

It's impossible to protect de single quote with \ because the querySelector won't find the good option...

If someone as an idea to help me...

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use CSS.escape() for this:

var NomClient = "L'ÉQUIPE";
var idClient = document.querySelector("#client option[value='" + CSS.escape(NomClient) + "']").dataset.value;

console.log(idClient);
<div id="client">
<select>
  <option value="L'ÉQUIPE" data-value="yes">L'ÉQUIPE</option>
  <option value="Libération">Libération</option>
  <option value="Marianne">Marianne</option>
</select>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Try this

function getSite() {
  var NomClient = $("#choix_client").val()

  var idClient = $("#client option").filter(function() { return this.textContent.trim() === NomClient }).val()

  $('#idClient').val(idClient);

  $.ajax({
    type: "POST",
    url: "getSite.php",
    data: 'client=' + idClient,
    success: function(data) {
      $("#choix_site").html(data);
    }
  });
}
   <option value="<?=$id?>"><?=$nom?></option> 
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!