How to call a js function on dynamically created input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • welkin
    New Member
    • Dec 2014
    • 1

    How to call a js function on dynamically created input?

    I am trying to call a js function on blur effect of dynamically created textbox. Code are as follows.

    js to add dynamic text box
    Code:
    {
    var el3 = document.createElement('input');					
    	el3.type = 'text';									
    	el3.name = 'children' + i;								
    	el3.id = 'children' + i;									
    	
    	//el3.setAttribute('onblur',"myFunction()");
    	el3.onblur = function(){myFunction();};
    	thirdCell.appendChild(el3);						
    	frm.h.value=i;										
    	i++;	
    
    }
    JS Function to be called on blur effect of above dynamically created element.

    Code:
    function myFunction()
    {
    var x = document.getElementById("child1").value;
    for(i=0;i<x;i++)
    {
    	var childAge =  prompt("Enter Child " + (i+1) + " age")
    	childageDetail.push(childAge);
    
    }
    //Loop for child age display
    for(ageDisplay = 1; ageDisplay < childageDetail.length; ageDisplay++)
    {
    	alert("Child "+ ageDisplay +" age "+ childageDetail[ageDisplay]);
    }
    
    }
    Last edited by Dormilich; Dec 22 '14, 12:41 PM. Reason: please use code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    1)
    Code:
    el3.onblur = function(){myFunction();};
    can be simplified to
    Code:
    el3.onblur = myFunction;
    2) I’m not entirely sure what your problem is.

    Comment

    Working...