Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <h3>jQuery.suggest.js</h3> <p>The discussion here has lead to the development of a jQuery plugin, which you can find here: <a href="http://polarblau.github.com/suggest/">http://polarblau.github.com/suggest/</a>.</p> <p>All code and examples below are therefore outdated but might still be interesting for some.</p> </blockquote> <hr> <p>I've been very interested in the outcome of this question, but it seems that hasn't been anything found yet. </p> <p>I've thought about writing my own solution for a little while now and I can tell you what I had in mind (and this is only in my head right now and definitely not anyhow tried out):</p> <p>HTML:</p> <pre><code>&lt;div id="search-container"&gt; &lt;input type="text" name="search" id="search" /&gt; &lt;input type="text" disabled="disabled" id="suggestion" /&gt; &lt;/div&gt; </code></pre> <p>CSS:</p> <pre><code>#container { position: relative; } #search { position: relative; color: #000; z-index: 10; background: transparent; // border, etc.... } #suggestion { position: absolute; top: 0; left: 0; z-index: 0; color: #ccc; border: none; // ... } </code></pre> <p>The use <strong>Javascript</strong> 'onkeydown' to match the first (sort criteria are important here) word from a list that shares the already typed letters in the beginning of the word and place it in the disabled input field <code>#suggestion</code>. If the user hits enter, the word from <code>#suggestion</code> will be transfered into the <code>#search</code> input field and possibly the form submitted.</p> <p>If I find the time I'll try to make it a working jQuery plugin — let's see. But maybe you get the idea?</p> <hr> <p><strong>EDIT</strong></p> <p>I've tried my idea and it seems to <a href="http://jsfiddle.net/cZb9r/3/">work in it's simplest form</a> quite okay. I'll release it as a small jQuery plugin soon at my <a href="https://github.com/polarblau/">github account</a>. I'll drop you a note here, once I'm done. Or go ahead and give it a crack yourself and let me know how it's coming.</p> <p>Here's the code that I ended up using (parts of it would probably be dynamically generated):</p> <p><strong>HTML:</strong></p> <pre><code>&lt;div id="search-container"&gt; &lt;input type="text" name="search" id="search" /&gt; &lt;input type="text" disabled="disabled" id="suggestion" /&gt; &lt;/div&gt; </code></pre> <p><strong>CSS:</strong></p> <pre><code>* { margin: 0; padding: 0; } #search-container { position: relative; } #search-container input { padding: 5px; font-size: 1.2em; width: 200px; } #search { position: relative; color: #000; z-index: 10; border: 1px solid #666; background: transparent; } #suggestion { position: absolute; top: 0; left: 0; z-index: 0; color: #ccc; background: transparent; border: 1px solid #fff; } </code></pre> <p><strong>JAVASCRIPT:</strong></p> <pre><code>$(function() { var haystack = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; $('#search').keyup(function(e) { // 'enter' key was pressed var $suggest = $('#suggestion'); var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { $(this).val($suggest.val()); $suggest.val(""); return false; } // some other key was pressed var needle = $(this).val(); // is the field empty? if (!$.trim(needle).length) { $suggest.val(""); return false; } // compare input with haystack $.each(haystack, function(i, term) { var regex = new RegExp('^' + needle, 'i'); if (regex.test(term)) { $suggest.val(needle + term.slice(needle.length)); // use first result return false; } $suggest.val(""); }); }); }); </code></pre>
    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