{"id":802,"date":"2004-05-24T12:50:37","date_gmt":"2004-05-24T12:50:37","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2004\/05\/copying_selections_with_javascript-shtml\/"},"modified":"2004-05-24T12:50:37","modified_gmt":"2004-05-24T12:50:37","slug":"copying_selections_with_javascript-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/","title":{"rendered":"Copying Selections With JavaScript"},"content":{"rendered":"<p>\nOne thing I&#8217;ve started to do for web based admin systems is a far greater use of JavaScript.\n<\/p>\n<p>\nOne task I find I do quite frequently is copying from a list of options to a sublist of selected options. For example, a list of all blog entries to a sublist of related blog entries.\n<\/p>\n<p>\nI do this by using a version of the following JavaScript. It takes a <code>&lt;select&gt;<\/code> HTML element with the property <code>multiple=\"multiple\"<\/code> set, and copies to another <code>&lt;select&gt;<\/code> <acronym title=\"HyperText Markup Language\">HTML<\/acronym> element, also with <code>multiple=\"multiple\"<\/code> set. I have a button set to execute the JavaScript via an <code>onClick<\/code> handler.\n<\/p>\n<p>\nLets have a quick look at the code&#8230;\n<\/p>\n<p>\nFirst we need to define our function. Here I&#8217;ve called it <code>doCopy<\/code> and it takes two parameters which are the two <code>&lt;select&gt;<\/code> elements from the <acronym title=\"Document Object Model\">DOM<\/acronym>, eg <code>document.formname.selectname<\/code>.\n<\/p>\n<div class=\"code\"><code>function doCopy(list1, list2) {<\/code><\/div>\n<p>\nNow we need to iterate over the first list to check each <code>&lt;option&gt;<\/code> to see if it has been selected.\n<\/p>\n<div class=\"code\"><code>for (var i=0; i&lt; list1.length; i++) {<br \/>\nif (list1.options[i].selected == true) {<br \/>\n<\/code><\/div>\n<p>\nIf the current <code>&lt;option&gt;<\/code> is selected, we need to iterate over the second list, making sure we don&#8217;t already have it in the list. I&#8217;m setting a temporary variable, <code>cancopy<\/code> that is set to false if we find we already have it in the second list.\n<\/p>\n<div class=\"code\"><code>var cancopy = true;<br \/>\nfor (var j=0; j&lt; list2.length; j++) {<br \/>\nif (list1.options[i].value == list2.options[j].value) {<br \/>\ncancopy = false;<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nIf <code>cancopy<\/code> is true, then we&#8217;re OK to copy the item over to the second list. To do this we have to create a new <code>Option<\/code> object. Let&#8217;s have a quick look at the <code>Option<\/code> class in JavaScript. We can constuct a new <code>Option<\/code> like this&#8230;\n<\/p>\n<div class=\"code\"><code>new Option(text, value, defaultSelected, selected)<\/code><\/div>\n<p>\nWhere <code>text<\/code> is the text to show, <code>value<\/code> is value, <code>defaultSelected<\/code> is a boolean defining if we want the defaultSelected property set, and <code>selected<\/code> is a boolean defining if we want the selected property set.\n<\/p>\n<p>\nWe need to initialise our new Option with the value and text of our original option from list one, and set it as selected. We insert this into the secondlist by adding it to the very of its array. This will grow the list automatically.\n<\/p>\n<div class=\"code\"><code>if (cancopy == true) {<br \/>\nlist2.options[list2.options.length] = new Option(list1.options[i].text, list1.options[i].value, false, true);<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nLets put this all together for our final code&#8230;\n<\/p>\n<div class=\"code\"><code>function doCopy(list1, list2) {<br \/>\nfor (var i=0; i&lt; list1.length; i++) {<br \/>\nif (list1.options[i].selected == true) {<br \/>\nvar cancopy = true;<br \/>\nfor (var j=0; j&lt; list2.length; j++) {<br \/>\nif (list1.options[i].value == list2.options[j].value) {<br \/>\ncancopy = false;<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\nif (cancopy == true) {<br \/>\nlist2.options[list2.options.length] = new Option(list1.options[i].text, list1.options[i].value, false, true);<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nHere is some example HTML that uses the JavaScript to copy from list1 to list2.\n<\/p>\n<div class=\"code\"><code>&lt;form name=\"mytestform\"&gt;<br \/>\n&lt;select name=\"list1\" multiple=\"multiple\"&gt;<br \/>\n&lt;option value=\"1\"&gt;One&lt;\/option&gt;<br \/>\n&lt;option value=\"2\"&gt;Two&lt;\/option&gt;<br \/>\n&lt;option value=\"3\"&gt;Three&lt;\/option&gt;<br \/>\n&lt;option value=\"4\"&gt;Four&lt;\/option&gt;<br \/>\n&lt;\/select&gt;<br \/>\n&lt;input type=\"button\" value=\"Copy\" onClick=\"doCopy(document.mytestform.list1,document.mytestform.list2);\" \/&gt;<br \/>\n&lt;select name=\"list2\" multiple=\"multiple\"&gt;<br \/>\n&lt;\/select&gt;<br \/>\n&lt;\/form&gt;<br \/>\n<\/code><\/div>\n<p>\nWe should now be able to copy between the lists when we click on the <code>Copy<\/code> button.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing I&#8217;ve started to do for web based admin systems is a far greater use of JavaScript. One task I find I do quite frequently is copying from a list of options to a sublist of selected options. For example, a list of all blog entries to a sublist of related blog entries. I &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Copying Selections With JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-802","post","type-post","status-publish","format-standard","hentry","category-dev"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Copying Selections With JavaScript - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copying Selections With JavaScript - Robert Price\" \/>\n<meta property=\"og:description\" content=\"One thing I&#8217;ve started to do for web based admin systems is a far greater use of JavaScript. One task I find I do quite frequently is copying from a list of options to a sublist of selected options. For example, a list of all blog entries to a sublist of related blog entries. I &hellip; Continue reading &quot;Copying Selections With JavaScript&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2004-05-24T12:50:37+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Copying Selections With JavaScript\",\"datePublished\":\"2004-05-24T12:50:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/\"},\"wordCount\":359,\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/\",\"name\":\"Copying Selections With JavaScript - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2004-05-24T12:50:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/copying_selections_with_javascript-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Copying Selections With JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copying Selections With JavaScript - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Copying Selections With JavaScript - Robert Price","og_description":"One thing I&#8217;ve started to do for web based admin systems is a far greater use of JavaScript. One task I find I do quite frequently is copying from a list of options to a sublist of selected options. For example, a list of all blog entries to a sublist of related blog entries. I &hellip; Continue reading \"Copying Selections With JavaScript\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/","og_site_name":"Robert Price","article_published_time":"2004-05-24T12:50:37+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Copying Selections With JavaScript","datePublished":"2004-05-24T12:50:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/"},"wordCount":359,"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/","name":"Copying Selections With JavaScript - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2004-05-24T12:50:37+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/copying_selections_with_javascript-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Copying Selections With JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}