Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery SelectBox
    text
    copied!<p>I wanted to customize a SelectBox with jquery and I have done this fake selectBox with Jquery but it is not working.</p> <p>It is supposed to set invisible the "select" tag and put instead the div with the ul inside.</p> <p>I'm newbie at Jquery so please if you could help me?</p> <p>Thanks in advance,</p> <p>Orangejuice.-</p> <p>This is the html:</p> <pre><code>&lt;label class="search-filter-tl"&gt;Sort by&lt;/label&gt; &lt;select class="select-sort-by" name="sort-by"&gt; &lt;option&gt;Relevance&lt;/option&gt; &lt;option value="alp-asc"&gt;Alphanumeric ASC&lt;/option&gt; &lt;option value="alp-desc"&gt;Alphanumeric DESC&lt;/option&gt; &lt;option value="pd-asc"&gt;Publish date ASC&lt;/option&gt; &lt;option value="pd-desc"&gt;Publish date DESC&lt;/option&gt; &lt;/select&gt; &lt;div id="replace-sort-by" class="replace"&gt; &lt;div class="current"&gt;Relevance&lt;/div&gt; &lt;ul id="sort-by-options" class="options"&gt; &lt;li rel="alp-asc" &gt;Alphanumeric ASC&lt;/li&gt; &lt;li rel="alp-desc"&gt;Alphanumeric DESC&lt;/li&gt; &lt;li rel="pd-asc"&gt;Publish date ASC&lt;/li&gt; &lt;li rel="pd-desc"&gt;Publish date DESC&lt;/li&gt; &lt;/ul&gt;&lt;!-- #options --&gt; &lt;/div&gt;&lt;!-- #replace-sort-by --&gt; </code></pre> <p>And this is the Jquery:</p> <pre><code>$(document).ready(function (){ //Onload:Run a function when the page is fully loaded including graphics. var selectSortBy = $('select.select-sort-by'); var replaceSortBySelect = $('div.replace'); selectSortBy.css('display', 'none'); replaceSortBySelect.css('display', 'block'); var lis = $('ul.options li'); for (var i = 0; i &lt;= lis.length; i++){ lis[i].click(function (){ //When you do a click in lis... selectSortBy.val($(this).attr()); //al valor del select asignale el valor del atribute del div $('ul.options').css('display','none'); //Do not show the option tag $('div.current').html($(this).html()); //Faked selected option that will be showned in the select input. }); } $(replaceSortBySelect).mouseover(function() { $('ul.options').css('display','block'); }); $(replaceSortBySelect).mouseleave(function() { $('ul.options').css('display','none'); }); }); </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