![]() |
help with adding/removing item from list
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> |
Well for starters, listbox isn't defined anywhere.
Add this to your remove function (like you did in your add function): var listbox = document.getElementById('MyListbox'); |
I updated the remove function, but how do you remove the selected value? I don't think the removeChild would be correct for this.
function removeItem(listbox) { var listbox = document.getElementById('MyListbox'); var i; for(i=listbox.options.length-1;i>=0;i--) { if(listbox.options[i].selected) listbox.removeChild(i); } } |
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
Is there a way to add just the text without the option value (just using the add button) and remove the item without the option value (just select from the pull down list and use the delete button from the keyboard or java delete button).
|
The functions and framework I spent time creating for you give you the capability to do anything you want.
If you want to add blank or pre-valued options with only a button, then go for it - the code is all there. |
| All times are GMT +1. The time now is 06:12 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.