Tuesday, February 26, 2008

HTML Dropdown Handling Javascript Functions

1.How to Remove an Option From HTML DropDown [removeOptionDsc]
The following method will help to remove one option in the Drop down by sending the Description in the Drop down.
Function Arguments
dd:- The Drop down object
dsc:- the description in the drop down which is to be deleted.
function removeOptionDsc(dd,dsc){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.childNodes(0).data==dsc){
dd.options[i]=null;
}
}
}

Example:
If one dropdown id is "users" with data
  1. Asker Ali M
  2. Haris M
  3. BrickRed
  4. Horizon
then user can call like
var ddObject= document.getElementById("users");
removeOptionDsc(ddObject,"Asker Ali M");

Then the dropdown will be
  1. Haris M
  2. BrickRed
  3. Horizon

2.To remove an Option by passing DropdownId.
Function Arguments
dd:- The Drop down object
val:- the option id which is to be deleted.

function removeOption(dd,val){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.value==val){
dd.options[i]=null;
}
}
}

3.How get the description of the Option by passing optionId.
Function Arguments
dd:- The Drop down object
val:- the option id to retrieve the DescriptionReturn type:
Description String.

function getDDDsc(dd,val){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.value==val){
return opt.childNodes(0).data;
}
}
}

4.How to clear a DropDown

Function Arguments
dd:- The Drop down object
function clearDD(dd){
if(dd==null) return;
var size = dd.options.length;
for (q = size; q >= 0; q--)
{
dd.options[q]=null;
}
}

1 comment:

Asker Ali M said...

Please Specify the Status
xmlObj.readyState
0: not initialized.
1: connection established.
2: request received.
3: answer in process.
4: finished.]