1

An external script that I use, generates divs of a certain class from time to time, it can happen unlimited amount of times. I'm trying to run a function each time such element is being created. I tried using $('selector').live(function(){}) and $('selector').waitUntilExists(function(){}) but it's only valid for the first time it's created. how can I create some kind of a listener that will call a function each time a new element of this class is being created?

Thanks

Yoav
  • 929
  • 2
  • 10
  • 25
  • 1
    check this post http://stackoverflow.com/questions/15268661/jquery-on-create-event-for-dynamically-created-elements – Osama Jetawe Apr 05 '15 at 12:26

1 Answers1

1

As you want to create elements dynamically.

You need to use Event Delegation. You have to use .on() using delegated-events approach.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

General Syntax

$(document).on(event, selector, eventHandler);

Ideally you should replace document with closest static container.

Satpal
  • 126,885
  • 12
  • 146
  • 163