Solved

jquery on click handler in an each loop

Posted on 2009-03-30
6
603 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
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 …
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…

758 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

27 Experts available now in Live!

Get 1:1 Help Now