By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

How to call a js function on dynamically created input?

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. {
  2. var el3 = document.createElement('input');                    
  3.     el3.type = 'text';                                    
  4.     el3.name = 'children' + i;                                
  5.     el3.id = 'children' + i;                                    
  6.  
  7.     //el3.setAttribute('onblur',"myFunction()");
  8.     el3.onblur = function(){myFunction();};
  9.     thirdCell.appendChild(el3);                        
  10.     frm.h.value=i;                                        
  11.     i++;    
  12.  
  13. }
JS Function to be called on blur effect of above dynamically created element.

Expand|Select|Wrap|Line Numbers
  1. function myFunction()
  2. {
  3. var x = document.getElementById("child1").value;
  4. for(i=0;i<x;i++)
  5. {
  6.     var childAge =  prompt("Enter Child " + (i+1) + " age")
  7.     childageDetail.push(childAge);
  8.  
  9. }
  10. //Loop for child age display
  11. for(ageDisplay = 1; ageDisplay < childageDetail.length; ageDisplay++)
  12. {
  13.     alert("Child "+ ageDisplay +" age "+ childageDetail[ageDisplay]);
  14. }
  15.  
  16. }
Dec 22 '14 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
1)
Expand|Select|Wrap|Line Numbers
  1. el3.onblur = function(){myFunction();};
can be simplified to
Expand|Select|Wrap|Line Numbers
  1. el3.onblur = myFunction;
2) Iím not entirely sure what your problem is.
Dec 22 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.