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

Sachin Tendulkar's emotional farewell Speech at Wankhede Stadium, Mumbai - Full Text and Video

Full Text and Video of Sachin Tendulkar's emotional farewell Speech on Nov 16, 2014 On November 16, 2013 - Sachin Ramesh Tendulkar played his last international cricket match against West-Indies at the at Wankhede Stadium, Mumbai. After the match he gave an emotional farewell speech. No one would be able forget the speech. The eyes of the gathered people were tearfull. Through out the speech Sachin thanked each and everyone who supported and encouraged him to build up his glorius career which spanned 24 years.

How to loop through the items in a select box using Javascript?

Looping through the items in a select box using JS Use the code given below to parse through the options in a select box and alert the Text and Value of each options. you can alter the function as per your need. sample code is given below: <form> <select id="test" name="test"> <option value="First Value" >First Text</option> <option value="Second Value" >Second Text</option> <option value="Third Value" >Third Text</option> <option value="Fourth Value" >Fourth Text</option> <option value="Fifth Value" >Fifth Text</option> </select> <input type="button" value="Check" onclick="loopSelectBox('test')" > </form> <script> function loopSelectBox(selectBoxId){ var obj=document.getElementById(selectBoxId); for(i=0; i < obj.options.length ;i++){ alert("Value :...

Autobiography by Cricket legend Sachin Tendulkar "Sachin Tendulkar - Playing it My Way : My Autobiography"

Autobiography by Cricket legend Sachin Tendulkar Cricket batting legend Sachin Ramesh Tendulkar's  autobiography was released on November 5, 2014 in a high profile launch function in Mumbai. . Earlier  on his Twitter page Sachin has announced the release date of his Autobiography " Sachin Tendulkar - Playing it My Way : My Autobiography "   as November 6th 2014 , but the book was released a day earlier. It took three years to write this book which he says as the Second innings of his life. Sachin Ramesh Tendulkar, the all time Genius of World Cricket was born on April 24, 1973 at Bombay (now Mumbai) in Maharashtra. He hold the record for scoring most runs and hundreds in Tests and ODIs. He scored a total of more than 34000 runs from One day Internationals (ODI's) and tests. He was part of the World cup winning team of 2011. He was a great all rounder. His  debut match was against    Pakistan  at Karachi during Nov 15-20, 1989. He played h...


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