N
N
Nikolai Vasilchuk2011-05-12 07:23:40
JavaScript
Nikolai Vasilchuk, 2011-05-12 07:23:40

jQuery ready for new elements?

By $(document).ready all select elements are processed .
After that, new selects can appear on the page, they can be created by javascript or received by ajax.
With live, you can assign some kind of event handler to them, but I need to handle them at the time of creation.
I cannot wedge into places where elements are created.
How to process new elements?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavlo Ponomarenko, 2011-05-12
@Anonym

Try to do like this -

var nodes = document.getElementsByTagName('select');

The nodes will have a live NodeList , which changes when the house-tree changes, so it's enough to check the length in the timeout
var nodes = document.getElementsByTagName('select');
var prev = 0;
setInteval(function () {
  if (nodes.length != prev) {
    prev = nodes.length;
    recount();
  }
}, 10);

Of course, if one select is removed and another is added, this option will not work and it is necessary to compare by content.

S
sajgak, 2011-05-12
@sajgak

setInterval to check for availability.
“I can’t wedge into places where elements are created.” can you all?

X
xdenser, 2011-05-12
@xdenser

As an option, override document.createElement and call your event after calling the old document.createElement. Then cling to this event and check what has been created there.
This needs to be checked for compatibility though, and may not work when elements are created via .innerHTML.

C
chikuyonok, 2011-05-12
@chikuyonok

DOM Level 2 has a DOMSubtreeModified event, try hanging it on documentin modern browsers.
www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings-mutationevents

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question