-1

I try to define my problem clearly using a use case, so here it is:

consider <a class="fruit" href=#>apple</a> and instead of apple could be any fruit. i want to declare a jQuery on listener like the one below that any time a dom element that has fruit class created and added any part of the body my listener do something to that element.

$(document).on("HERE IS THE QUESTION",".fruit",function(){ do something to the fruit here });

I know there are some alternative ways for reaching the result but this is how i want to solve this problem due to some complexity reasons.

This only needs to work in Chrome.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
Roozbeh Sharafi
  • 347
  • 1
  • 6
  • 21
  • also i know forexample i can use $(document).on("mousemove","body",function(){ here searching for any $(".fruit") but it is really inefficient and not a universal solution}); – Roozbeh Sharafi May 17 '14 at 21:30
  • 1
    Why would you need to listen for events? What changes these elements? – Bergi May 17 '14 at 21:31
  • 1
    You need to decide what event you want to listen to — in this case, why not call the function in the event that triggers to addition/creation of the fruit class? – Terry May 17 '14 at 21:33
  • That's the problem i have :) the code for other parts is minified and old, and my client doesn't want to re write the code base and he just want's some few changes so practically we can not easily find where the elements are being created (i'm sure it's more thant 1place that creates the elements) and editing is not that easy – Roozbeh Sharafi May 17 '14 at 21:34
  • 2
    @RoozbehSharafi - are you seriously saying that the client who wants this change only has the minified JS and doesn't have the original source? Are you sure they are the legitimate owners of the code? – jfriend00 May 17 '14 at 21:46
  • @jfriend00 unfortunately it's true and because of lack of knowledge and developers without work ethic. Now they had been work with this system for 2years, the previous programmer is not available and they are stuck here. – Roozbeh Sharafi May 17 '14 at 21:50
  • 1
    Do none of these solutions work for you (I'm sort of gently pimping my own answer, obviously I suppose, but the others *should* work for you): "[jQuery "on create" event for dynamically created elements](http://stackoverflow.com/questions/15268661/jquery-on-create-event-for-dynamically-created-elements)"? – David Thomas May 17 '14 at 22:09
  • @DavidThomas yeah it worked! so why don't you just submit it and i will accept it as the right answer for my question? (the on DOMNodeInserted i mean) – Roozbeh Sharafi May 17 '14 at 22:15
  • Because the fact that it worked, and solved your problem, suggests that this question is a duplicate. – David Thomas May 17 '14 at 22:21
  • 1
    @DavidThomas - Slight issue here. You marked this a duplicate of another question (which is similar in nature, though the specifics are quite different) that has no answer that is as appropriate as the answer here, particularly given that this is a Chrome-only question where mutation events could be used. How is that supposed to be handled when the one you marked it a dup of doesn't actually have an answer that solves the OP's issue? – jfriend00 May 17 '14 at 23:29
  • 1
    @jfriend00: y'know, I'm beginning to see why mods are so hesitant to use Mjölnir...reopened, but I remain (at least semi) convinced that this is a dupe of that one, or similar (that I remember seeing, but can't now find). As to 'how is it a dupe?' The OP said "yeah it worked!" so I assumed it did solve his issue. – David Thomas May 17 '14 at 23:35
  • @jfriend00 thanks for your flexible attitude. Strict attitude just same as Duplicate questions may kill stackoverflow. – Roozbeh Sharafi May 17 '14 at 23:47
  • 1
    Dups are a bit hard to manage here. Some are obvious, many are not. There are different questions with the same answer, there are mostly similar questions that require a different answer and there are questions that really have no good answer provided that it doesn't seem to help anyone to mark things a dup of. I think I'll go do a little more reading over in meta about dups. – jfriend00 May 17 '14 at 23:51

1 Answers1

1

If you're trying to monitor when a DOM element with a certain class name is created and added to the DOM, then you can't do that using straight jQuery as there is no good cross browser (that works in older browsers) way of doing that and it isn't something jQuery has tried to solve.

There is a newish interface (requires IE 11) called mutation observers (MDN reference here) that can allow you to monitor for certain types of DOM changes where you could get an event when items are added to the DOM and you could check if they were your class. If you really only need it to work in Chrome, this should be just what you want.

There is also the old standby (that's bad for battery life) technique of using a setInterval() to check the DOM regularly for changes.

Or, you could also describe in more detail and from a higher level what problem you're really trying to solve and we might be able to offer other ideas.

Here's some sample code using mutation objservers:

$(document).ready(function() {
    // create an observer instance
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                if (mutation.addedNodes[i].className === "fruit") {
                    console.log("found fruit", mutation.addedNodes[i]);
                }
            }
      });    
    });

    // configuration of the observer:
    var config = { childList: true, subTree: true };

    // pass in the target node, as well as the observer options
    observer.observe($("#container")[0], config);    
});

And, a working demo: http://jsfiddle.net/jfriend00/zxEXp/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • actually the cross-browser development is not the problem it is a web application that is used inside the company everybody is using Chrome. and yes setting the interval is one of good solutions and in contrast with my mousemove example in my comments it is more efficients but anyway every time i have to check all .fruits and also it may be not very live :) so it's a blade you could cut yourself with both sides! if you choose a big interval result in using less memory and not live results and if you choose an small interval you will rich the live performance but the trade off is the memory – Roozbeh Sharafi May 17 '14 at 21:58
  • 1
    @RoozbehSharafi - If it's only Chrome, then why not use mutation events? – jfriend00 May 17 '14 at 22:04
  • @RoozbehSharafi - Added sample code that works in Chrome for a MutationObserver to my answer. – jfriend00 May 17 '14 at 23:44
  • 00 Yes! That exactly what i am looking for. – Roozbeh Sharafi May 17 '14 at 23:45