Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO`1.` I'm not sure how using `addEventListener` helps separate Javascript from the HTML more than something like `.onclick`. `2.` One declaration isn't necessarily cleaner, especially when you make it one large line. `3.` "array" is not a reserved word..."Array" is...Javascript is case-sensitive. `4.` `===` is not always the correct choice - this is the perfect example because some people may not be sure what `.value` returns (which is always a string) or `.split`, but if you're comparing them, there's no reason not to use `==`.
      singulars
    2. CO`1.` Initially the OP wanted to "do it in java-script[_sic_]" so in that case `addEventListener` would be the alternative to `onload`. But with jQuery then `$(document).ready( function() { ... });` already separates the JavaScript from the HTML. `2.` You can put it on multiple lines, but IMO one declaration is cleaner. `3.` I stand corrected. `4.` `===` is the correct choice. Since `.val()` and `.value` returns strings, you should be comparing a string with it.
      singulars
    3. CO@rink.attendant.6 - Thanks for the post - the variable declaration piece is definitely cleaner and making the string the parameters of the function and doing the split inside makes a lot of sense. The reason I asked for JS was because it's easier for me to understand...the Jquery is just so foreign. Although I will say, did end up using Ian's $(document).ready idea because I was already using it (I didn't even realize it). Again....I'm pretty new. Really appreciate the help though.
      singulars
 

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