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

How to delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***

What are the Income Tax Rates for Indian citizens for Financial Year 2017-2018?

Income Tax Slab and Rates given below are for Indian citizens of age less than 60. This rates are applicable for the Financial Year 2017-2018 Income Tax Slab Rates Financial Year 2017-2018 Assessment Year 2018-19 Income Tax Slab Rates SLAB 1 Individuals whose total income not exceeding Rs. 2,50,000 ( 2.5 lakhs ) They are exempted from paying income tax.


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