2

I'm a bit of a newbie working on a web application. Currently building one JSP page with a select box that selects a value upon page load based upon a value inserted through EL. This is done using the following JQuery function:

$('.interestBox').each(function(){

    if ($(this).val() ===('${personalFile.interest}')) {
        $(this).attr({selected: 'selected'});
    } else {
        $(this).removeAttr('selected');
    }
});

This was working fine... until somebody added a field of interest "Don't Know" and another one "Mr. "T"" (The 'T' is with quotation marks - can't just change from ' to ''). This is all data that is pulled from a DB so I don't have much control. But here's what the first line in the function will look like for these two samples:

        if ($(this).val() ===('Don't Know')) {

If I change from single quote to double quote, it will not solve the problem. See below:

    if ($(this).val() ===("Mr. "T"")) {

Now, the JS script doesn't work. Anyone have any idea how to deal with this? Thanks.

  • 2
    Your second "this is what they'll look like" is incorrect. The string is wrapped in single quotes, the T is surrounded by double quotes, there's no issue in that particular case. – Anthony Grist Apr 24 '14 at 09:17
  • 1
    You are right - what I meant was that if I would change from single quotes to double quotes it would not solve the problem. Edited to reflect this. Thanks. – user3175265 Apr 24 '14 at 09:23
  • Did you oversee the single quote in "don't know"? because that also will break your code. – Kai Apr 24 '14 at 09:31
  • That is EXACTLY where the code breaks! This is my question! – user3175265 Apr 24 '14 at 09:35
  • 1
    possible duplicate of [How to escape a single quote from within a JSP?](http://stackoverflow.com/questions/9708242/how-to-escape-a-single-quote-from-within-a-jsp) – Anthony Grist Apr 24 '14 at 09:40
  • Okey.. So if you switch the quotes to: "Don't know" 'Mr. "T"'. OR use quotes consistently and addslashes (or use str.replace(/"/g, '"')) if you really have no other choice. – Kai Apr 24 '14 at 09:40
  • Thanks for the link. I'll use Apache's StringEscapeUtils like suggested there. – user3175265 Apr 24 '14 at 09:52

0 Answers0