Accepted answer

The issue here is that IDs are treated as strings in the DOM, so here: index + 1 !== selectedButton (Where selectedButton is set from

You are comparing a number to a string, and they will never be equal, so !== will always return true


function example(e) {
  console.log(1 ===
<input id="1" onClick="example(event)" placeholder="Click me" />

You will need to either:

  • Convert the index + 1 to a string before comparing the values
  • Parse the ID as a number before the comparison
  • Parse the ID as a number before storing it to selectedButton

I would recommend replacing:

const clcikedDivId =


const clcikedDivId = parseInt(

Related Query

More Query from same tag