Click to See Complete Forum and Search --> : Removing and Reset an Element


said_fox
08-26-2003, 05:59 PM
As we know if we have an element has the name, for example, fff and this element may be a form element like select. we can remove it dynamically as follows,

arr = document.getElementsByName("fff");
arr[0].innerHTML="";


A typical Example for this is demonstrated in the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
function remove_it(){
arr = document.getElementsByName("fff");
arr[0].innerHTML="";
}
</script>
</head>
<body>
<form>
<select name="fff">
<option>op1
<option>op2
<option>op3
</select>
<input type="checkbox" onClick="remove_it()">
</form>

Ok till this point every thing goes well. The problem starts when I want to re add or redisplay the removed element.
I tried to store the value of arr[0].innerHTML in a global variable, var x, but when I assign arr[0].innerHTML = x.
nothing is happened. ofcourse I make some kind of modification on the remove_it() function.
Do any one has some advice or idea?:confused:

Fang
08-27-2003, 03:13 AM
If it's just options you want to change:

var obj=document.formname.selectname.options[index];
// store data
arr[index]=[obj.text, obj.value];
// remove option
document.formname.selectname.options[index]=null;
// restore option
document.formname.selectname.options[index]=new Option(arr[index][0], arr[index][1]);

Don't forget to check that "index" contains a valid value.