javascript - Focus event not working on dynamic content - Stack Overflow

I am adding input dynamically using jQuery. After that i am trying to use focus event to generate yet a

I am adding input dynamically using jQuery. After that i am trying to use focus event to generate yet another input group. Here is the jsfiddle (/) and the snippet is below

HTML:

    <div class='row' id='addChild'>
    <input type='name' name='child0' id='child0'>
    <input type='name' name='phone0' id='phone0'>
    </div>

JS:

  $(document).ready(function(){
     $("#child0").focus(function() {
         $('#addChild').append("<input type='name' name='child1' id='child1'><input type='name' name='phone1' id='phone1'>");
     });
     $("#child1").focus(function() {
         $('#addChild').append("<input type='name' name='child2' id='child2'><input type='name' name='phone2' id='phone2'>");
     });
   });

On focus of #child0 it will create #child1. But, on focus of #child1, it does nothing

I am adding input dynamically using jQuery. After that i am trying to use focus event to generate yet another input group. Here is the jsfiddle (http://jsfiddle/sk8UG/) and the snippet is below

HTML:

    <div class='row' id='addChild'>
    <input type='name' name='child0' id='child0'>
    <input type='name' name='phone0' id='phone0'>
    </div>

JS:

  $(document).ready(function(){
     $("#child0").focus(function() {
         $('#addChild').append("<input type='name' name='child1' id='child1'><input type='name' name='phone1' id='phone1'>");
     });
     $("#child1").focus(function() {
         $('#addChild').append("<input type='name' name='child2' id='child2'><input type='name' name='phone2' id='phone2'>");
     });
   });

On focus of #child0 it will create #child1. But, on focus of #child1, it does nothing

Share Improve this question edited Jul 18, 2014 at 11:37 gzix asked Jul 18, 2014 at 11:17 gzixgzix 2693 silver badges21 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 8

use event delegation

$('#addChild').on("focus","#child1",function() {

 // do your work here 
});

NOTE: You have to use event delegation on element which are created at run time since those elements are not available during document ready

Document Says

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

Try this code:

$(document).on('focus','#child1',function() {
    $('#addChild').append("<input type='name' name='child1' id='child1'><input type='name' name='phone1' id='phone1'>");
 });

Working Demo

Binding event before element in not created and not presend in DOM. Event Delegation is method for jquery event on dynamic content.

Detilas

As you asked explain delecation Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

Below code will work fine

    $('#addChild').on("focus","#child1",function() {
    $(this).append("<input type='name' name='child2' id='child2'><input type='name' name='phone2' id='phone2'>");
     });

When you are binding the on focus event, the element with the id child1 has not been created yet. Event listeners can only be bound on existing elements. As Bala explained, event delegation is the way to go.

As an alternative you could bind the new event after creating a new element

$(document).ready(function(){
     $("#child0").focus(function() {
         $('#addChild').append("<input type='name' name='child1' id='child1'><input type='name' name='phone1' id='phone1'>").focus(function() {
         $('#addChild').append("<input type='name' name='child2' id='child2'><input type='name' name='phone2' id='phone2'>");
     });
   });
 $('#addChild').on("focus","#child1",function() {

  // do your work here 
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744983078a4604454.html

相关推荐

  • javascript - Focus event not working on dynamic content - Stack Overflow

    I am adding input dynamically using jQuery. After that i am trying to use focus event to generate yet a

    21小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信