2

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?

0

4 Answers 4

4

You can use this inside the event handler to refer to the current element

$($clicker[i]).on('click', function () {
    $(this).animate({
        'left': '0'
    });
});

Why is your solution not working


The given code can be changed as(there is no need to has a switch statement here)

var $texte = $('.text-bg');

$texte.children('.text-top-left').click(function () {
    var $this = $(this),
        flag = $this.data('animToggle');
    $this.data('animToggle', !flag);
    $this.animate({
        'left': flag ? '0' : '-100px'
    })
})

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

7 Comments

I edited my code, it some how is not working. Could you take a look at it?
[.toggle(function, function, ... )] is removed in jQuery 1.9
The edit you posted works, the problem is I have atleast 2 cases. That's why I needed the switch. One time I have to animate from the right, and the other time from the left side. Is there a way to change your code to this? And could you post a resource so I could understand what you did?
@FrederikWitte if you can post the updated code, we can have a look
Hey, I just used ur code($this insead of $($texte[i])). I think its okay like it is right now. The only problem is its flickering now. When I first click it it works just fine, but when I click it again, its jumping left and right.
|
3

You need to use this, In event handler it refers to element which executed the event.

Use

$($clicker[i]).on('click', function(){
    $(this).animate({'left':'0'}); //Used this here
});

Your code can be simplified as However .toggle(function, function, ... ) removed in jQuery 1.9

$('.text-bg > .text-top-left').on('click', function() {
    var toggle = $(this).data('toggle');
    $(this).data('toggle', !toggle);
    $(this).animate({
        'left': toggle ? '-100px' : '0'
    });
});

1 Comment

I edited my code, it some how is not working. Could you take a look at it?
1

You should be able to attach the handler to all jQuery result elements at once:

$clicker.on('click', function(){
    $(this).animate({'left':'0'}); //Used this here
});

With your updated code, you should use the selection facilities already provided by jQuery:

$('.text-bg > .text-top-left').on('click', function() {
  $(this).toggle(
    function() {
     $(this).animate( {'left':'0'})}, 
      function() {
       $(this).animate({'left':'-100px'})
      });
    });
 });

To add a second case (for another class) just 'double' the code with the alternative behavior.

Comments

0

You can use

$($texte[i]).on('click', function(){
    $(this).animate({'left':'0'});
});

OR simply assign the loop variable to a local variable INSIDE the loop:

var j = i;
$($texte[j]).on('click', function(){
    $($texte[j]).animate({'left':'0'});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.