Hi,

I know it's possible to add actions for elements that are not in DOM yet, like
$(element).live('event',function() {
//code here
});

Is there any way to execute the code without the event, or give it an event like 'exists' ? Like, when object is added, the function executes automatically, so i don't have to click or mouseover the element or something like that.

Thanks

Recommended Answers

All 7 Replies

Member Avatar for stbuchok

What do you mean by, "when object is added"? What object and added to what? Live and bind are both used for event binding.

Can you try to explain better what you are trying to do and give a good example please.

For example, i create a new element with document.createElement('div');
I need to execute an action when the element is created. As far as i have found, .live and .bind allows only events on action (like i click the new element or something), but i need an event when the element is first introduced.

P.S. The element creation code is out of my reach. An action after document.createElement('div'); won't be an option.

Member Avatar for stbuchok

The only way I can think of (and this is ugly and this is assuming you know what type of element. ie div, input, a, hr, table...) is to have a setInterval that is constantly checking the number of those elements on the page and when there is one more element than the last, execute some code.

Of course this is all on the assumption also that you don't have access to the code that adds the element to the page.

Well, the code that creates element is an external script (mailchimp), which posts my form to it's page, generates stuff there and inserts response into my page.

Ask the developers. Perhaps there is a way to register a callback, so you can get notified when something is inserted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.