Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda