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

Deep-sea Anglerfish Black Seadevil Scary looking creature Video

Deep-sea Anglerfish are the strange and elusive creature that are very rarely observed in their natural habitat. Fewer than half a dozen have ever been captured on film or video by deep-diving research vehicles.They are mostly  found in tropical to temperate waters of the Indian,Pacific and Atlantic Oceans.

How to use WiFi adapter on Ubuntu 16.04 desktop PC - Realtek RTL8188EUS 802.11n Wireless USB Network Adapter Driver installation

Installation of Realtek RTL8188EUS 802.11n Wireless USB Network Adapter on Desktop PC having Ubuntu 16.04 OS My PC is running in Ubuntu 16.04 OS, recently I thought of using a dongle wife adapter to access our home's Wifi network. For this I used Realtek RTL8188EUS 802.11n Wireless USB Network Adapter ( Model No: OT-WUA950NM ) This small device cost around Rupees 250/- to Rs 300/- in India. I did the following steps for installation of this Realtek Nano Wifi Adapter: Plug Realtek RTL8188EUS 802.11n Wireless USB Network Adapter to your PC's USB port, Take the terminal application and run the command "lsusb" to list the plugged in usb devices: Myhome:~$ lsusb Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. R...

Cheap Tourist boat service in Alapuzha - Enjoy the beauty of Aleppey Back waters using govt owned Tourist boat services

Feasible Tourist boat services in Alapuzha Aleppey / Alappuzha is one of the beautiful places in kerala. Aleppey district is famous for it amazing backwaters , you can enjoy house boat rides here. Kerala State Water transport department  (Kerala SWTD) provides facilities for tourists to enjoy the beauty of backwater by paying much less charge when compared to private boat services. There are boat services from Aleppey boat station which takes tourists to inner parts of the backwaters.


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