amitava mozumder amitava mozumder - 23 days ago 7
jQuery Question

is there a way to run the jquery.live function without any event

The live function of jQuery is triggered with an event
The api documentation is here

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

$(".xyz").live('event', function(){
});


I want the above function to run without an event , i.e as soon as the class xyz is dynamically created.

I have tried this without the event parameter

$(".xyz").live( function(){
});


but it doesn't work !!

adding

Answer

I don't know exactly what you want to do, but I'll make assumptions.

Scenario 1

In order to execute a function inside jQuery, you need to tell jQuery when to execute the function, otherwise it cannot know. One way to do this is:

$(document).ready(function () {
    $('.xyz').css('color', 'red');
    // more code, anything, functions, calculations, etc.
});

The code inside that function will be executed as soon as your DOM is ready (is safe to manipulate). You can put $('.xyz').css('color', 'red'); also outside the ready() event and it will also be executed. The "event" this time is when the executions of the page reaches that line of code. For example, if you put this line before your HTML, you don't have guarantee it will work.

Scenario 2 - onClassChange event

If you are looking for an event called something like onClassChange, it does not exits on jQuery. An approach to that problem could be this:

  • Create a timer which runs infinitely
  • On each run, check if that object has your class ( $(obj).hasClass('xyz') )
  • If yes, do whatever you want to do inside the if ()

Also, since you said the class xyz is created dynamically, you can try to run your function immediately after you create your class in your code.

For more about onClassChange event, there are long discussions you can check on this site.