javascript - create a new text field when click on previous text field -
i want create new text field while clicking on created text field. have written function working problem is, should create new text field when clicking on last one, example if there 3 text fields , clicking on first 2 not add new text field. clicking on third text field can create new. following code not working (its java script function):
var fieldcount=1; function addinputfield(count) //on add input button click { alert(fieldcount); alert(count); if(fieldcount == count) { fieldcount++; var field = "#textfield_"+count; $node = '<input id="textfield_'+fieldcount+'" type="text" name="textfield" onclick="addinputfield(fieldcount);" />'; $(field).after($node); } };
there 1 text field in begging when page loads is:
<input id="textfield_1" type="text" name="textfield" onclick="addinputfield(fieldcount);" />
you need unbind click event textbox after has been clicked.
html:
<input id="textfield_1" type="text" />
javascript:
$(document).ready(function () { $("#textfield_1").click(function () { addinputfield(fieldcount); }); }); var fieldcount = 1; function addinputfield(count) //on add input button click { //alert(fieldcount); //alert(count); if (fieldcount == count) //max input box allowed { fieldcount++; //text box added increment //add input box var field = "#textfield_" + count; $node = '<input id="textfield_' + fieldcount + '" type="text" name="textfield" />'; $(field).unbind("click").after($node); $('#textfield_' + fieldcount).click(function () { addinputfield(fieldcount); }); //text box increment } };
working example: http://jsfiddle.net/bz5q4/
you use focus
instead of click
ensure if user tabs field 1 created.
Comments
Post a Comment