Condition is not taken into account in JavaScript

Viewed 26

I made a form with HTML on Symfony. With JavaScript, I am adding a new input every time I click on the button with the id addwishedlanguage. From the first click on the button, a new button appears, the one with the following id: removewishedlanguage. This one allows me to remove inputs that have been added. I am trying to hide this button when I end up to have one input left (the initial one). So I created idcounter in order to be aware when there is only one input left and I created the following condition: idcounter = 1. But the problem is that this condition is not taken into account. My button is hidden too soon from the first time I click on it.

What did I miss?

var idcounter = 1;
var id = "user_wishedlanguages" + idcounter;

document.getElementById("addwishedlanguage").onclick = function() {
    console.log(idcounter)
    const wishedlanguageselect = document.getElementById("user_wishedlanguages");
    const newwishedlanguageselect = wishedlanguageselect.cloneNode(true);
    newwishedlanguageselect.id = id;
    document.querySelector('.wishedlanguagelist').appendChild(newwishedlanguageselect);
    document.getElementById("removewishedlanguage").style.display = "initial";
    idcounter++
}

document.getElementById("removewishedlanguage").onclick = function() {
    console.log(idcounter)
    idcounter--
    document.getElementById(id).remove();
    if(idcounter = 1) {
        document.getElementById("removewishedlanguage").style.display = "none";
    }
}
0 Answers
Related