I can’t use quotes on a function value"Uncaught Syntaxerror: Invalid or Unexpected token"

Asked

Viewed 78 times

1

The function does not let you pass the "Mark" as value because of the use of quotation marks, any idea how I can solve (without removing the quotation marks)? Code :

<<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        function muda(x){
            document.getElementById('campo').innerHTML = x;
        }
    </script>
</head>
<body>

    <button onclick="muda('\"Marco\"')">Clicar</button>
    <div id="campo" style="width: 100px; height: 100px; background-color: green;"> Oá</div>

</body>
</html>
  • 1

    The thing about &quot; solved your problem?

  • yes solved the problem

2 answers

0


You can use &quot; to print the double quotes:

function muda(x){
    document.getElementById('campo').innerHTML = x;
}
<button onclick="muda('&quot;Marco&quot;')">Clicar</button>
<div id="campo" style="width: 100px; height: 100px; background-color: green;"> Oá</div>

0

Use single quotes to delimit the attribute and double quotes in the function, the one going in the string must be escaped:

function muda(x){
   document.getElementById('campo').innerHTML = x;
}
<button onclick='muda("\"Marco\"")'>Clicar</button>
<div id="campo" style="width: 100px; height: 100px; background-color: green;"> Oá</div>

If you wanted to send simple quotes in the string, it would be the other way around:

onclick="muda('\'Marco\'')"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.