Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Updated Answer 2015</h2> <p>As this question is still visited very often and due to some requests in the comments, I've revisit my code. You can still find my original answer below.</p> <p><strong>HTML</strong></p> <pre><code>&lt;button class="language_selector"&gt;Choose Language&lt;/button&gt; &lt;ul class="languages"&gt; &lt;li&gt;&lt;a href="/en"&gt;English&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/de"&gt;Deutsch&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;article&gt; &lt;h1&gt;More Content&lt;/h1&gt; &lt;/article&gt; </code></pre> <p><strong>JavaScript</strong></p> <pre><code>var trigger = $('.language_selector'); var list = $('.languages'); trigger.click(function() { trigger.toggleClass('active'); list.slideToggle(200); }); // this is optional to close the list while the new page is loading list.click(function() { trigger.click(); }); </code></pre> <p><strong>CSS</strong></p> <pre><code>.language_selector { width: 200px; background: #222; color: #eee; line-height: 25px; font-size: 14px; padding: 0 10px; cursor: pointer; } .languages { display: none; position: absolute; margin: 0; background: #dddddd; } .languages &gt; li { width: 200px; background: #eee; line-height: 25px; font-size: 14px; padding: 0 10px; cursor: pointer; } .languages &gt; li:hover { background: #aaa; } </code></pre> <p><strong>Demo</strong></p> <p><a href="http://jsfiddle.net/v60ppkw9/" rel="noreferrer">Try before buy</a></p> <hr> <h2>Original Answer From 2013</h2> <p>I would do it like this:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var nav = $('#nav'); var selection = $('.select'); var select = selection.find('li'); nav.click(function(event) { if (nav.hasClass('active')) { nav.removeClass('active'); selection.stop().slideUp(200); } else { nav.addClass('active'); selection.stop().slideDown(200); } event.preventDefault(); }); select.click(function(event) { // updated code to select the current language select.removeClass('active'); $(this).addClass('active'); alert ("location.href = 'index.php?lang=" + $(this).attr('data-value')); });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>h2 { width: 200px; background: #222; color: #eee; line-height: 25px; font-size: 14px; padding: 0 10px; cursor: pointer; } ol.select { display: none; } ol.select &gt; li { width: 200px; background: #eee; line-height: 25px; font-size: 14px; padding: 0 10px; cursor: pointer; } ol.select &gt; li:hover { background: #aaa; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;h2 id="nav"&gt;Choose Language&lt;/h2&gt; &lt;ol class="select"&gt; &lt;li data-value="en"&gt;English&lt;/li&gt; &lt;li data-value="de"&gt;Deutsch&lt;/li&gt; &lt;/ol&gt;</code></pre> </div> </div> </p> <p>This one adds a class the the selected element. This works, if you stay on that very page after the select and don't use <code>location.href</code>.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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