skip to Main Content

When I select a name from dropdown where it has an apostrophe
Example: Brian O’Connar

My JS code will bring Brian O and rest of the characters after apostrophe gets cuts.

My Original JS code to load the data —

else if(newClaimCols[key]== "assigned_to_full_name_primary") {
  var selectedText =$("#claim" + newClaimCols[key] + "filter").val() 
  //Brian O

  if (selectedText!==null && selectedText!=="" ) {
    var temp1 = $("#claim" + newClaimCols[key] + "filter").val();
    for (var key in temp1) {
      temp.push(temp1[key]);
    }
  }
}

In above case .val() selects only the text before apostrophe

2nd case
I tried .text() please refer below code

else if (newClaimCols[key] == "assigned_to_full_name_primary") {
  var selectedText = $("#claim" + newClaimCols[key] + "filter option:selected").text();//Brian O'Connar
  if (selectedText!==null && selectedText!=="" ) {
    var temp1 =$("#claim"+newClaimCols[key]+ "filter option:selected").text();
    temp.push(temp1);
  }
}

In this case i am getting Brian O’Connar but if i select multiple names Brian O’Connar and Nishinoya’O in dropdown it comes like

Example: Brian O’ConnarNishinoya’O

not sure how to handle this.

Please let me know how can i completely load the name with the apostrophe or a solution for 2nd case .text() when multiple name gets selected.

2

Answers


  1. Chosen as BEST ANSWER

    I cleaned up my HTML option tag from this

    optstring += "<option value='" + key +'='+  val + "'>" +val+ "</option>";
    

    to this

    optstring += '<option value="' + key +'='+  val + '">' +val+ "</option>";
    

    Now the value from html i am getting to JS is accepting apostrophe


  2. To properly handle the apostrophe and preserve the complete name, you can use JavaScript’s encodeURIComponent() function to encode the selected text before pushing it into the temp array. This will ensure that the apostrophe (and any other special characters) are correctly escaped. By encoding the selected text using encodeURIComponent(), you can safely pass it as a parameter or store it in an array without any issues. When you need to use the value later, you can decode it using decodeURIComponent().I added it to the code, I hope this solves your problem.

    else if (newClaimCols[key] == "assigned_to_full_name_primary") {
      var selectedText = $("#claim" + newClaimCols[key] + "filter option:selected").text();
      if (selectedText !== null && selectedText !== "") {
        var temp1 = $("#claim" + newClaimCols[key] + "filter option:selected").text();
        temp.push(temp1);
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search