Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

javascript - jquery delegate onload event-handler for newly created elements

I am delegating event handler to elements that are not yet created like this:

$('body').on('change', '#some_elementid', function() {
  //actions
});

this is working,

But I want actions to run right after the element is created without any other events. load doesnot seem to work..

what is possible here to achieve onload ?

UPDATE:

<div id="some_elementid">
    <button id="addmore">+ add more</button>
</div>

by clicking on + add more, another container will be inserted into #some_elementid which will have some fields. I want to prepopulate those fields during inserting.

without prepopulation, it will look like:

<div id="some_elementid">
    <div>
      <input type="text" id="id_0">
      <input type="text" id="id_1">
      ...
    </div>
    <button id="addmore">+ add more</button>
</div>

I want to manipulate during onload and show like:

 <div id="some_elementid">
    <div>
      <input type="text" id="id_0">
    </div>
    <button id="addmore">+ add more</button>
</div>

JSfiddle: http://jsfiddle.net/yugaj2x2/

I want to hide "two" while inserting. because later on clicking some other radio button, "two" will show up, but not now. this is what I am trying to achieve

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could capture event on all browsers which support it:

document.addEventListener(
    'load',
    function(event){
        var elm = event.target;
        if( elm.id === 'some_elementid'){ // or any other filtering condition
            // do some stuff
        }
    },
    true // Capture event
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...