Solved

jquery on click handler in an each loop

Posted on 2009-03-30
6
604 Views
Last Modified: 2012-05-06
Dear all,

I don't know why but my on click handler does not work.

It is never triggered.

I also tried with a

$(this).click(

but it was the same.

The loop works fine and the elements are removed.

But I would like to remove only the one element that has been clicked on, not all of them.

// Clear the displayed content

      $(this).find('span.elearning_question_dropped_content').find('.elearning_question_dropped_item').each(function(i, element) {

alert("This alert displays the element fine " + element);

// TODO why the on click does not work?

        $(this).bind(

          "click",

          function() {

alert('This alert is never displayed... why..?');

alert('clicked on id ' + element.attr('id'));

        });

        $(this).html('');

        $(this).remove();

      });

Open in new window

0
Comment
Question by:stephaneeybert
  • 4
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 24017730
You're binding a function to the click event of the element in the foreach loop (it doesn't get triggered) and after that the element is removed.

In order to trigger an event you have to use the trigger or triggerHandler method.
Just move the remove method into your function bound to the click event.

See code
// Clear the displayed content

      $(this).find('span.elearning_question_dropped_content').find('.elearning_question_dropped_item').each(function(i, element) {

    $(this).bind("click", function() {

        $(this).remove();

    });

});

Open in new window

0
 

Author Comment

by:stephaneeybert
ID: 24023706
Hi Albert,

Here is how I did:
      $(this).find('span.elearning_question_dropped_content').find('.elearning_question_dropped_item').each(function(i, element) {
        $(this).bind(
          "click",
          function() {
          $(this).html('');
          $(this).remove();
        });
      });

And it works fine.

But I don't know why it now works and not before.

Why the alert was not triggered before ?

Thanks
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 50 total points
ID: 24023815
Hey Stephane

You're looping through all elements with class elearning_question_dropped_item.
Each element will have it's click event bound to a funtion but the element gets deleted in the same loop.
Since it gets deleted you can't click it hence the alert won't be displayed.

NB
Do you see the difference in my and your solution? Exactly the same but you're clearing the contents first which is not realy needed because it gets removed.
You can simply remove the element.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:stephaneeybert
ID: 24023816
The first click does not delete the element.

It seems like the first click gets the focus.

Then fron the second click on the clicks do delete the elements.

How to fix this so as to have the first click delete the clicked element ?
0
 

Author Comment

by:stephaneeybert
ID: 24023834
Okay, I will remove the clearing of content.

Thanks for the explanation.
0
 

Author Closing Comment

by:stephaneeybert
ID: 31564233
Hi Albert, sorry for the delay, I'm on holidays in Egypt..

If you happen to have a clue about the focus issue I mentioned feel free :-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now