Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A correct translation would be something like:</p> <pre><code>var selects = document.getElementsByTagName('select'), options = document.getElementById('townid').getElementsByTagName('option'), options = Array.prototype.slice.call(options), //2 lines only for readability tmp = document.createDocumentFragment(); for(var i = 0, l = options.length; i &lt; l; i++) { var option = options[i], text = option.innerText || option.textContent; if(text.indexOf('Central') &gt; -1) { tmp.appendChild(option); } } for(var i = 0, l = selects.length; i &lt; l; i++) { var select = selects[i], opts = select.getElementsByTagName('option'); if(opts.length &gt; 1) { select.insertBefore(tmp.cloneNode(true), opts[1]); } else { select.appendChild(tmp.cloneNode(true)); } } </code></pre> <p><a href="http://jsfiddle.net/wfqMC/" rel="nofollow"><strong>DEMO</strong></a></p> <p>This could be <strong>simplified a lot</strong> depending on the markup (and optimized depending on the browser (e.g. support for <code>querySelectorAll</code>)). E.g. if you know that there will always only be <em>one</em> option that contains "Central" and whether there exists only one <code>select</code> element or not.</p> <p>Here is a stripped down version for one <code>select</code> element, known size of the list (i.e. > 1) and only one option that contains <code>Central</code>. So basically just reordering the option:</p> <pre><code>var options = document.getElementById('townid').getElementsByTagName('option'); for (var i = 0, l = options.length; i &lt; l; i++) { var option = options[i], text = option.innerText || option.textContent; if (text.indexOf('Central') &gt; -1) { if (i &gt; 1) { option.parentNode.insertBefore(option, options[1]); } break; } } </code></pre> <p><a href="http://jsfiddle.net/wfqMC/3/" rel="nofollow"><strong>DEMO</strong></a></p> <p><strong>Update:</strong></p> <p>If the option's text should be exactly <code>Central</code>, compare the text normally:</p> <pre><code>if(text === 'Central') </code></pre>
 

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