Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Calculators     
Home  »     »  Dynamic control generation - Javascript code

Dynamic control generation - Javascript code

Tuesday, March 1, 2011

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>



How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
Dynamic control generation - Javascript code.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



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 »