Skip to main content

Dynamic control generation - Javascript code

The JS code for generating controls dynamically.
Copy and paste the code and save it as an HTML file,
take it on the browser to know the working of the script.

The add Ctrl function will duplicate the TR of the table.

On clicking the link "Add More Contact Persons" the new row
of control will be created.

On clicking the " Remove" link the corresponding row will be deleted.



////////////////////////////////////////

<script>
function addCtrl>(ctrl_StartID, CtrlArray,startDivId, ContainerId,ctrlLabel, DivHeight,fieldLength, enableValidator){
/*
if there are more text controlls in a single div you can seperate
that by "*=*"
for example when we call this function to print 3 text boxes in a div with height25px
you can call like this
onclick="addCtrl('txtName*=*txtCab*=*txtLoc','arr_Name*=*arr_Cab*=*arr_Loc','divSpecial','span1','SpecName*=*CabSeat*=*CabLoc','25px'.'150px','0*=*1*=*2')"
The value '0*=*1*=*2' corresponsd to param 'enableValidator' is for handling the numeric and phone format enabling
*/
if(document.getElementById(ContainerId)){
var obj=document.getElementById(ContainerId);
var arrDiv=document.getElementsByTagName("DIV");
var cnt=0;
for(i=0 ; i < arrDiv.length ;i++){
if(arrDiv[i].id.indexOf(startDivId)==0 )
cnt++;
}
newcnt=cnt;
var newdiv = document.createElement('DIV');

newdiv.id =startDivId+newcnt;
newdiv.style.height=DivHeight;
var arrctrl_StartID=ctrl_StartID.split("*=*");
var arrCtrlArray=CtrlArray.split("*=*");
var arrctrlLabel=ctrlLabel.split("*=*");
var arrEnableValidator=enableValidator.split("*=*");
var strValidator="";

var arrCnt=arrctrl_StartID.length;
var innerStr="";

for(i=0;i < arrCnt ; i++){

if(arrEnableValidator[i]==1)
strValidator=" onkeyUP=\" isNumber('"+arrctrl_StartID[i]+"_"+newcnt+"',1);\" ";
else if(arrEnableValidator[i]==2)
strValidator=" onfocus=\"toNonFormatedStyle('"+arrctrl_StartID[i]+"_"+newcnt+"')\" onkeyUP=\" isNumber('"+arrctrl_StartID[i]+"_"+newcnt+"',0);\" onblur=\"toPhoneFormat('"+arrctrl_StartID[i]+"_"+newcnt+"')\" ";
else
strValidator="";

innerStr=innerStr +"<label> "+arrctrlLabel[i]+"</label>  <input id='"+arrctrl_StartID[i]+"_"+newcnt+"' type='text' name='"+arrCtrlArray[i]+"[]' style='width:"+fieldLength+"' "+strValidator+" >  ";
if( i == parseInt(arrCnt)-1 )
innerStr=innerStr+"<a onclick=remCtrl('"+newdiv.id+"','"+ContainerId+"'); class=link >Remove</a>";
}
newdiv.innerHTML = innerStr;
obj.appendChild(newdiv);
}
if(document.getElementById(arrctrl_StartID[0]+"_"+newcnt)){
document.getElementById(arrctrl_StartID[0]+"_"+newcnt).focus();
}
}



////////////////////////////////////////




function remCtrl(divid,containerId){
if(document.getElementById(divid) && document.getElementById(containerId)){
document.getElementById(containerId).removeChild(document.getElementById(divid));
}
}


function showOtherDiv(id,ftVal,hgt,ctrlwidth){
var strInnerContent=""
var divObj=document.getElementById('divContainer_'+id);
var stat='none';
var strId='chk_FTOther_'+id;
if( document.getElementById( strId ) ){
var obj=document.getElementById(strId);
if(obj.checked){
divObj.style.display='block';
strInnerContent="\n<span id=\"spanOther_"+id+"\">\n<div id=\"divOther_"+id+"\" style=\"height:"+hgt+";\">\n <label>  Other "+ftVal+" details </label>  \n<input id=\"txtOther__"+id+"\" type=\"text\" name =\"txt_other_"+id+"[]\" style=\"width:"+ctrlwidth+";\" >\n </div> </span> \n<div id=\"remdiv_"+id+"\" ><a onclick=\"addCtrl('txtOther__"+id+"','txt_other_"+id+"','divOther_"+id+"','spanOther_"+id+"','Other "+ftVal+" Details ','"+hgt+"','"+ctrlwidth+"');\" class=link >Add More </a></div>";
document.getElementById('divContainer_'+id).innerHTML=strInnerContent;
}
else{
divObj.innerHTML="";
divObj.style.display='none';
}

}


}



////////////////////////////////////////



function isNumber( obj,permitDecimal ){

var val= document.getElementById(obj).value;
var te=val.match(/\d+\.?\d*/);
document.getElementById(obj).value=te;
val=te;

if(val!=""){
if( isNaN(val) ){
endOFString=val.length-1;
val=val.substring(0,endOFString);
}
if(!permitDecimal){
if(val){
val=val.toString();
document.getElementById(obj).value=val.replace(/\./g,'');
}
}else{
document.getElementById(obj).value=val;
}

}
}



////////////////////////////////////////





function toNonFormatedStyle( obj ){

val= document.getElementById(obj).value;

if(val.indexOf('(')==0){
val=val.replace(/[\(\)\-\s]/g,'');
document.getElementById(obj).value=val;
}
}



////////////////////////////////////////




function toPhoneFormat( obj ){
val= document.getElementById(obj).value;

arrChars=val.split('');
formatedString='';
for(i=0;i < val.length;i++){
if(i==0)
formatedString+='(';
if(i==3)
formatedString+=') ';
if(i==6)
formatedString+='-';

formatedString+=arrChars[i];
}
document.getElementById(obj).value=formatedString;
}

////////////////////////////////////////




</script>
<style>
.txt{
font-size:14;
color:maroon; font-family:Arial;
}

.rad{
font-size:12; background-color:#ECEFFF; height:16px
color:#161616; font-family:Arial; padding-left:4px; padding-right:2px;float:left; border:1px solid gray; width:45px; margin-right:2px; padding-bottom:4px;
}

.rad input {margin-top:3px;}


</style>








<table>

<tr><td colspan="2" >
<span id="contactpersons_span">
<div id="divContactpersons" style="height:25px">
<label> Name</label>  <input id="txtpersonname" type="text" name="personname[]" maxlength="25" style="width:125px">  
<label>Relation</label>  <input id="txtrelation" type="text" name="personrel[]" maxlength="25" style="width:125px">  
<label>Telephone</label>  <input id="txtpersontel" type="text" name="persontel[]" maxlength="20" style="width:125px" onfocus="toNonFormatedStyle('txtpersontel')" onkeyUP=" isNumber('txtpersontel',0);" onblur="toPhoneFormat('txtpersontel')">  
<label>Mobile</label>  <input id="txtpersonmob" type="text" name="personmob[]" maxlength="20" style="width:125px" onfocus="toNonFormatedStyle('txtpersonmob')" onkeyUP=" isNumber('txtpersonmob',0);" onblur="toPhoneFormat('txtpersonmob')">  
</div>
</span>
<div id="contactpersonremdiv" ><a onclick="addCtrl('txtpersonname*=*txtrelation*=*txtpersontel*=*txtpersonmob','personname*=*personrel*=*persontel*=*personmob','divContactpersons','contactpersons_span','Name*=*Relation*=*Telephone*=*Mobile','25px','125px','0*=*0*=*2*=*2');" class=link>Add More Contact Persons</a></div>
</td></tr>

</table>

Comments

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog