samedi 1 décembre 2018

.setAttribute(id,element.id) is not a function

When i click on a smallDiv i want to add its id to the BigDiv. Unfortunately, my console says that element.setAttribute is not a function. I console log my SmallDiv id and it works when i click on it (in comments). I also tried without the if statement but it doesn't work too.

Here is my code :

var smallDiv = document.getElementsByClassName("small");
var bigDiv = document.getElementsByClassName("big");

for(let i=0;i<smallDiv.length;i++){
smallDiv[i].addEventListener("click",function(){
    if(smallDiv[i].hasAttribute('id')){
    bigDiv.setAttribute('id',smallDiv[i].id);
    //console.log(smallDiv[i].id);
    }
});
};

Thanks for your help !

Aucun commentaire:

Enregistrer un commentaire