BakedVanilla
BakedVanilla

Reputation: 37

Quotation marks in textarea text interfere with code

I have a text area in HTML with a paragraph of text. I then have a jQuery function that looks at the text area and compares it with a paragraph defined in the function. If the two paragraphs are identical, then the text area box changes background color to green, otherwise it is red. However, the problem is that in the jQuery function the paragraph contains quotation marks that interfere with the quotation marks that identify a string. Is there a way to get around this?

Upvotes: 1

Views: 2306

Answers (1)

Fhtagn
Fhtagn

Reputation: 773

You need to use something like a regex to escape the quotation marks in the textarea's value and then compare it to an escaped string:

$(document).ready(function(){
  $('.submit').on('click', function(){
  var text = $('textarea').val().replace(/"/g, """).replace(/'/, "'");
        if (text === "I said, "Hello!"") {
        $('textarea').css('background-color', 'green');
    }
  });
});

https://jsfiddle.net/c3gathkc/

Upvotes: 2

Related Questions