I'm having trouble with my function. I need a for loop to attach a click event to my elements. Now it sadly only adds the click event to the last element.
I did it like this right now:
var $texte = $('.text-bg');
$texte = $texte.children();
for(i=0;i<$texte.length;i++){
switch($($texte[i]).attr('class')){
case 'text-top-left':
console.log($texte[i]);
$($texte[i]).on('click', function(){
$($texte[i]).toggle(
function(){$($texte[i]).animate({'left':'0'})},
function(){$($texte[i]).animate({'left':'-100px'})}
);
});
break;
}
}
But somehow the click event doesnt save. So when it goes into the second loop in the for function, the first click event is being overwritten. How to prevent this?