Friday, September 5, 2008

creating listbox items and sending to another listbox using javascript

//Html Code










































//javascript code


// JScript File
function fnMoveItems(lstbxFrom,lstbxTo)
{
debugger;
var varFromBox = document.all(lstbxFrom);
var varToBox = document.all(lstbxTo);
if ((varFromBox != null) && (varToBox != null))
{
if(varFromBox.length < selectedindex ="="">= 0)
{
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox
}
setSize(varFromBox,varToBox);
}
return false;
}
function fnMoveMultItems(lstbxFrom,lstbxTo)
{
debugger;
var varFromBox = document.all(lstbxFrom);
var varToBox = document.all(lstbxTo);
var len = varFromBox.length -1;
if ((varFromBox != null) && (varToBox != null))
{
if(varFromBox.length < 1)
{
alert('There are no items in the source ListBox');
return false;
}

for(i=0; i<=len;i++){
var j=0;
varToBox.appendChild(varFromBox.item(j));
}
selectNone(varFromBox,varToBox);
setSize(varFromBox,varToBox);

}
return false;
}
function selectNone(list1,list2){
list1.selectedIndex = -1;
list2.selectedIndex = -1;
addIndex = -1;
selIndex = -1;
}
function getSize(list){
var len = list.childNodes.length;
var nsLen = 0;
//nodeType returns 1 for elements
for(i=0; i if(list.childNodes.item(i).nodeType==1)
nsLen++;
}
if(nsLen<2)
return 2;
else
return nsLen;
}
function setSize(list1,list2){
list1.size = getSize(list1);
list2.size = getSize(list2);
}

No comments: