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

289
Vistas
Onclick & boolean switch

I have tried to make an function with a onclick that when you click it, it will change the value from 'suspended' in true (this is about suspending a website user account)

suspended = false; 
type user = User['suspended'];

function blokkeerFunctie() {
  // get user & user element
  document.getElementById('userInfo') && document.getElementById('blokkeren');
  // blocks user when clicked
  if (document.getElementById('blokkeer')?.addEventListener('click', blokkeerFunctie)) {
    type user = !User['suspended'];
  } else {
    // deblocks user when clicked
    document.getElementById('deblokkeer')?.addEventListener('click', blokkeerFunctie);
    type user = User['suspended'];
  }
  console.log('blokkeerFunctie');
}
blokkeerFunctie();

I thought with !User i will reverse the boolean value from false in true, but that code isn't even read. ▼

'user' is declared but never used.ts(6196)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You shouldn't put event listeners in your conditional if/else in this way. Here's how I would approach what you're trying to accomplish. You will need to add types to these variables, but you'll get the basic logic here.

let User = {
  suspended: true
};
let button = document.querySelector('#suspender');

function setSuspendButton() {
  button.innerText = User['suspended'] ? 'Unsuspend' : 'Suspend';
}

window.addEventListener('load', () => {
  button.addEventListener('click', blokkeerFunctie)
  setSuspendButton();
})

function blokkeerFunctie() {
  User['suspended'] = !User['suspended'];
  setSuspendButton();
}
<button id='suspender'></button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

type user = creates a new type, not a value. It's unused in each branch of the if because you just create a new type, named user which shadows the type of the same name from the parent scope, which is never used by anything.

Furthermore, this line does nothing:

document.getElementById('userInfo') && document.getElementById('blokkeren');

This line gets up to two references to DOM elements, but doesn't save them anywhere, or perform any logic on them.


I think you want something more like this?

const user = {
  name: 'Joe',
  suspended: false
}

function blokkeerFunctie() {
  // block/deblocks user when clicked
  if (document.getElementById('blokkeer')?.addEventListener('click', blokkeerFunctie)) {
    user.suspended = !user.suspended // toggle `suspended` back and forth
  }
  console.log('blokkeerFunctie');
}
blokkeerFunctie();

Working example

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