2

A simple plugin

jQuery.fn.cleanHouse = function() {
    //do stuff
}

$('.dirty').cleanHouse();

How can we make sure future elements $('.dirty') will automatically call cleanHouse() ?

I'm looking solution do something with the plugin itself

angry kiwi
  • 9,824
  • 23
  • 98
  • 151
  • 1
    how will future elements come to fruit? this dictates how you might create your own trigger (of a custom event) which you could bind live() to – jenson-button-event Feb 18 '11 at 23:35

4 Answers4

0

It's not possible to achieve this. The only way the plugin can get called in the future is through an event.

angry kiwi
  • 9,824
  • 23
  • 98
  • 151
0

is jquery's live() function what you need?

http://api.jquery.com/live/

or this

jQuery - Fire event if CSS class changed

Community
  • 1
  • 1
jenson-button-event
  • 17,272
  • 9
  • 83
  • 149
0

The problem with .live() is that you can only use it in connection with an event, and there is no built-in event that fires when you add elements to the DOM.

But here's a detailed explanation on how you make that happen with a custom "create" event: http://www.erichynds.com/jquery/jquery-create-event/

0

I think what you are looking for is a combination, try something like this:

$.fn.cleanHouse = function() {
    return $(this).live('click', function() { 
        //do stuff
    });
}
$('.dirty').cleanHouse();
Mouhannad
  • 2,244
  • 1
  • 14
  • 12