Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Alright so I'm going to try to address all the parts of your question in my answer.</p> <p>As Ian had mentioned, there is a syntax error in your <code>for</code> statement.</p> <p>On second thought I'll give the code then do the explanation. I have included the entire HTML document for purposes of clarity:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=en-CA&gt; &lt;meta charset=utf-8&gt; &lt;title&gt;JavaScript in multi-select&lt;/title&gt; &lt;script&gt; function selectItems(stringFromDB) { 'use strict'; var select = document.getElementById("multiselectid"), stringArray = stringFromDB.split(","), count = 0, i; for(count = 0; count &lt; stringArray.length; count += 1) { for(i = 0; i &lt; select.options.length; i += 1) { if(select.options[i].value === stringArray[count]) { select.options[i].selected = true; } } } } window.addEventListener('load', function() { 'use strict'; // The "string from DB" would be the parameter here selectItems( 'hockey,volleyball,football'); }, false); &lt;/script&gt; &lt;body&gt; &lt;div&gt; &lt;select id='multiselectid' multiple&gt; &lt;option value="hockey"&gt;Hockey&lt;/option&gt; &lt;option value="golf"&gt;Golf&lt;/option&gt; &lt;option value="volleyball"&gt;Volleyball&lt;/option&gt; &lt;option value="football"&gt;Football&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; </code></pre> <p>Now for the explanation.</p> <ol> <li><p>Instead of using the traditional <code>onload</code> event attribute on the body element to run the script on page load (as you had wanted), I attached an event handler with the event <code>load</code> to the <code>window</code> DOM element. Event handlers are more useful as they help separate the JavaScript from the HTML (and many other reasons). You can read more about event handlers here: <a href="https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener" rel="nofollow">https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener</a></p></li> <li><p>In the function <code>selectItems()</code>, I have put all of the variables onto one declaration to keep the code clean.</p> <p><strike>3. To avoid potential issues with arrays, I have called the variable <code>stringArray</code> instead. <code>array</code> is a reserved word in JavaScript and could cause issues with some browsers, although I'm not completely sure which ones (if any).</strike></p></li> <li><p>ALWAYS, ALWAYS, ALWAYS use <code>===</code> instead of <code>==</code>. That way you are sure the comparison is strict and correct (i.e. both things being compared are of the same type, in this case: strings).</p></li> <li><p>It is a good idea to force the handling of your JavaScript functions in strict mode by using <code>'use strict;'</code>. This way the browser will stop (and throw exceptions) if it detects something that is not allowed, and thus allowing you to write better JavaScript code.</p></li> </ol> <p>Hope this helped, feel free to ask more questions if you're still confused!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload