Search code examples
javascriptescapingsingle-quotes

Javascript : Escaping single quote in function parameter doesn't work


In a span tag, I have a function where parameter value can be a string with single quote.

But even if I escape single quote by antislash, browser console displays error.

Here my code :

<html>
<body>

<script type="text/javascript">

function check(val)
{
    console.log(val);
}

</script>   

<span contenteditable onkeydown='check("foo\'foo");'>abc</span>

</body>
</html>

Why the escaping doesn't work ?


Solution

  • <span contenteditable onkeydown="alert('foo\'foo');">abc</span>
    

    will do the trick.