Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!