-1

I have some french texts which is using single quote and double quote in select items option as function parameter.

  $.get(submit_url + '/spelist', {
      action: 'spelist',
      list: 'a'
  }, function(msg) {
    $('#ac1').html('');
    for(be in msg.db) {
      var part_one = msg.db[be].id;
      var part_two = msg.db[be].list;
      $('#ac1').append('<option  style="padding:10px 0px;" onclick="button_5a1_list_submit(' + part_one + ',\'' + part_two + '\');">' + part_two + '</option>');       
    }
  });

which is creating as below:

onclick="button_5a1_list_submit(20, 'value has single quote 'again text');"

But the single quote or double quote is causing unexpected identifier how do you kill this?

enter image description here enter image description here

9
  • 2
    I would instead bind an .on('click') to the options, that should solve your problem.
    – sharf
    May 20, 2015 at 22:57
  • JSON.stringify to turn any old toothpick mess into valid js string literals, switch the attrib delims to apos
    – dandavis
    May 20, 2015 at 22:57
  • can you use onclick on <options>? how would that work in mobile?
    – dandavis
    May 20, 2015 at 22:58
  • How to do the .on('click') with JavaScript ? @sharf
    – user285594
    May 20, 2015 at 23:22
  • Do you not want the jQuery method?
    – sharf
    May 20, 2015 at 23:35

1 Answer 1

0

Since most browsers won't support onClick for an option anyway, you'll need to bind to the select element.

What I suggest is adding a data attribute to your option such as:

$("#ac1").append("<option data-val=\""+part_one+"\">"+part_two+"</option>");

and binding to the select like this:

$("#ac1").on("change", function(){
    var sel = $(this);
    var val = sel.val();
    var data = sel.find("option:selected").data("val");
    button_5a1_list_submit(data, val);
});

This is a cross browser solution that will get the part_one (data-val) and the part_two (option value) when you make a selection from your select element.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.