<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: add code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 01:10:56 GMT</pubDate>
    <description>DZone Snippets: add code</description>
    <item>
      <title>HTML/JavaScript - Select list - Add/Remove Options (DOM)</title>
      <link>http://snippets.dzone.com/posts/show/4442</link>
      <description>from http://www.mredkj.com/tutorials/tutorial005.html&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Overview&lt;br /&gt;&lt;br /&gt;    * Insert Before Selected - A new option is created and added above the selected option (as determined by selectedIndex). If none are selected, then no option is added.&lt;br /&gt;    * Remove Selected - Deletes the selected option (or options) from the list. If no options are selected, no options are deleted.&lt;br /&gt;    * Append Last - No matter what is selected, a new option is added at the end.&lt;br /&gt;    * Remove Last - No matter what is selected, the last option is deleted from the list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Explanation&lt;br /&gt;&lt;br /&gt;According to DOM Level 1, the following is the syntax for the add and remove methods in HTMLSelectElement:&lt;br /&gt;&lt;br /&gt;void add(in HTMLElement element, in HTMLElement before) raises(DOMException);&lt;br /&gt;void remove(in long index);&lt;br /&gt;&lt;br /&gt;The add method takes two arguments: the element to add, and the element to insert before. The spec also says you can add to the end of the list by passing null as the second argument.&lt;br /&gt;&lt;br /&gt;The remove method just takes a number: the index of the option to be removed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The JavaScript&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script language="JavaScript" type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;var count1 = 0;&lt;br /&gt;var count2 = 0;&lt;br /&gt;&lt;br /&gt;function insertOptionBefore(num)&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.selectedIndex &gt;= 0) {&lt;br /&gt;    var elOptNew = document.createElement('option');&lt;br /&gt;    elOptNew.text = 'Insert' + num;&lt;br /&gt;    elOptNew.value = 'insert' + num;&lt;br /&gt;    var elOptOld = elSel.options[elSel.selectedIndex];  &lt;br /&gt;    try {&lt;br /&gt;      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE&lt;br /&gt;    }&lt;br /&gt;    catch(ex) {&lt;br /&gt;      elSel.add(elOptNew, elSel.selectedIndex); // IE only&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionSelected()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  var i;&lt;br /&gt;  for (i = elSel.length - 1; i&gt;=0; i--) {&lt;br /&gt;    if (elSel.options[i].selected) {&lt;br /&gt;      elSel.remove(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function appendOptionLast(num)&lt;br /&gt;{&lt;br /&gt;  var elOptNew = document.createElement('option');&lt;br /&gt;  elOptNew.text = 'Append' + num;&lt;br /&gt;  elOptNew.value = 'append' + num;&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE&lt;br /&gt;  }&lt;br /&gt;  catch(ex) {&lt;br /&gt;    elSel.add(elOptNew); // IE only&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionLast()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    elSel.remove(elSel.length - 1);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;//--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The HTML&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="insertOptionBefore(count1++);" /&gt;&lt;br /&gt;Insert Before Selected&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionSelected();" /&gt;&lt;br /&gt;Remove Selected&lt;br /&gt;&lt;br /&gt;&lt;select id="selectX" size="10" multiple="multiple"&gt;&lt;br /&gt;&lt;option value="original1" selected="selected"&gt;Orig1&lt;/option&gt;&lt;br /&gt;&lt;option value="original2"&gt;Orig2&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="appendOptionLast(count2++);" /&gt;&lt;br /&gt;Append Last&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionLast();" /&gt;&lt;br /&gt;Remove Last&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 22 Aug 2007 12:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4442</guid>
      <author>bradalyst (brad)</author>
    </item>
  </channel>
</rss>
