-2

How do I make this work? <a href='javascript:func("Jack'S Birthday")'>Jack's Birthday</a>

jforex78
  • 315
  • 2
  • 11

4 Answers4

1

Do as follows:

// How about using variables instead?
var emily = "Emily'S Birthday"
var birthdays = {
  john: "John'S Birthday"
}

function func(val) {
  console.log(val);
}
<!DOCTYPE html>
<html>

<body>
  <a href='javascript:func("Jack&apos;S Birthday")'>Jack's Birthday</a>
  <br>
  <a href="javascript:func('Norman\'S Birthday')">Norman's Birthday</a>
  <br>
  <a href="javascript:func(emily)">Emily's Birthday</a>
  <br>
  <a href="javascript:func(birthdays.john)">John's Birthday</a>
</body>

</html>

Explanation:

  1. Keep single quotes within double quotes when you escape using backslash \
  2. Use double quotes within single quotes when you use $apos;
  3. Best of all, use variables, they ease a lot of pain -

    a. You don't have to go into html to modify values,
    b. these can be declared at one place, or a single file,
    c. They can be fetched from back-end
    d. Best yet, no need to deal with quotes!

Abhijit Kar ツ
  • 1,732
  • 1
  • 11
  • 24
1

Apparently you can't escape characters within HTML attributes. The proper way to go would be to use HTML entities like &apos; :

<a href='javascript:console.log("&apos;")'>click me</a>

See How to properly escape quotes inside html attributes?.

0

If possible, do it this way

<html>
  <header>
    <script>
    function func(x) {
      alert(x);
    }</script>
  </header>
  <body>
    <a href="javascript:func('Norman\'S Birthday')">Birthday</a>
  </body>
</html>
D. Seah
  • 4,472
  • 1
  • 12
  • 20
0

Convert to NCR. This should work.

<html>
    <a href='javascript:func("Norman&#39;S Birthday")'>Birthday</a>
</html>

Or.

<html>
    <a href='javascript:func("Norman&#x27;S Birthday")'>Birthday</a>
</html>