liftthis
12-16-2008, 03:58 PM
Need some help with getting the remove selected item code to work:
<html>
<head>
<title>(Type a title for your page here)</title>
<script language="javascript" >
function addNewItem()
{
// Retrieve the elements from the document body
var textbox = document.getElementById('MyTextbox');
var listbox = document.getElementById('MyListbox');
// Now we need to create a new 'option' tag to add to MyListbox
var newOption = document.createElement('option');
newOption.value = textbox.value; // The value that this option will have
newOption.innerHTML = textbox.value; // The displayed text inside of the <option> tags
// Finally, add the new option to the listbox
listbox.appendChild(newOption);
}
function removeItem(listbox)
{
var i;
for(i=listbox.options.length-1;i>=0;i--)
{
if(listbox.options[i].selected)
listbox.removeChild(i);
}
}
</script>
</head>
<input id="MyTextbox" type="textbox" />
<input type="button" value="Add Item" onclick="java script:addNewItem()" />
<input type="button" value="Remove Item" onclick="java script:removeItem()" />
<br /><br />
<select id="MyListbox" size="10">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
<option value="bananas">Bananas</option>
</select>
</form>
</body>
</html>
<html>
<head>
<title>(Type a title for your page here)</title>
<script language="javascript" >
function addNewItem()
{
// Retrieve the elements from the document body
var textbox = document.getElementById('MyTextbox');
var listbox = document.getElementById('MyListbox');
// Now we need to create a new 'option' tag to add to MyListbox
var newOption = document.createElement('option');
newOption.value = textbox.value; // The value that this option will have
newOption.innerHTML = textbox.value; // The displayed text inside of the <option> tags
// Finally, add the new option to the listbox
listbox.appendChild(newOption);
}
function removeItem(listbox)
{
var i;
for(i=listbox.options.length-1;i>=0;i--)
{
if(listbox.options[i].selected)
listbox.removeChild(i);
}
}
</script>
</head>
<input id="MyTextbox" type="textbox" />
<input type="button" value="Add Item" onclick="java script:addNewItem()" />
<input type="button" value="Remove Item" onclick="java script:removeItem()" />
<br /><br />
<select id="MyListbox" size="10">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
<option value="bananas">Bananas</option>
</select>
</form>
</body>
</html>