Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

375
Visualizações
function return 'Uncaught SyntaxError: expected expression, got }' error in onClick event

I want to change a Javascript variable in onClick action.

but my console return this error message: Uncaught SyntaxError: function statement requires a name

I don't know why JavaScript behaves like this:

My codes:

const table_body = document.querySelector('.table_body');

let color = 550;

const createTable = function() {

  const tableAdd_HTML = '<button class="funcBtn" onclick="javascript:(function() { alert("color changed");    color = 3; })()">Run js function in onClick action</button>';

table_body.innerHTML = tableAdd_HTML;

}
.funcBtn{
  width: 160px;
  height: 80px;
  background-color: red;
  color: white;
  position:absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<button type="button" onClick="createTable()">Create Table</button>

<div class="table_body"></div>

It is very interesting for me to know why this code behaves like this, Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first quote in alert("color changed") is matching the quote in onclick=", so it's ending the attribute.

Use single quotes in the attribute so they don't match the attribute delimiter.

Then you need to ensure that this doesn't conflict with the quotes that delimit the entire HTML string. You could escape the single quotes, but a simple solution is to make the string a template literal.

const table_body = document.querySelector('.table_body');

let color = 550;

const createTable = function() {

  const tableAdd_HTML = `<button class="funcBtn" onclick="javascript:(function() { alert('color changed');    color = 3; })()">Run js function in onClick action</button>`;

table_body.innerHTML = tableAdd_HTML;

}
.funcBtn{
  width: 160px;
  height: 80px;
  background-color: red;
  color: white;
  position:absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<button type="button" onClick="createTable()">Create Table</button>

<div class="table_body"></div>

You can avoid all these quoting problems by using DOM objects instead of HTML strings and inline JavaScript.

const table_body = document.querySelector('.table_body');

let color = 550;

const createTable = function() {
  const button = document.createElement("button");
  button.classList.add("funcBtn");
  button.addEventListener("click", function() {
    alert("color changed");
    color = 3;
  });
  button.innerText = "Run js function to change color";

  table_body.innerHTML = '';
  table_body.appendChild(button);
}
.funcBtn {
  width: 160px;
  height: 80px;
  background-color: red;
  color: white;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<button type="button" onClick="createTable()">Create Table</button>

<div class="table_body"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda