1

Hello I have a problem with this code:

for(var element in partsListById)
{
    $('.title#'+partsListById[element]).on('click touch', function(e){
        $('.part#'+partsListById[element]).slideToggle();
    });
}

the partsListById array contains a string list of the name of the objects I want to target.

I don't understand why the code above doesn't work and this one works:

$('.title#sensor').on('click touch', function(e){
    $('.part#sensor').slideToggle();
});

Where is my mistake?

I'm realy new in JS.

Can you help me?

Thank you

Matio
  • 33
  • 5

2 Answers2

0

Use let or const so that the variable is local to the loop block.

for(const element in partsListById)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

This is because the loop is not executed, that is, the Value of "partsListById" is empty. First Check the Value "partsListById"

console.log(partsListById);

if is null the loop is not executed.