<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: auto-complete code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 01:29:08 GMT</pubDate>
    <description>DZone Snippets: auto-complete code</description>
    <item>
      <title>1st attempt at building an autocomplete dhtml user interface</title>
      <link>http://snippets.dzone.com/posts/show/4990</link>
      <description>This following html, javascript, and css code is used to build a basic auto-complete user interface. The next step will be to build the back-end server which will handle AJAX requests.&lt;br /&gt;&lt;br /&gt;file:autocomplete.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"&gt;&lt;br /&gt;  &lt;head&gt;&lt;br /&gt;    &lt;meta http-equiv="Content-Type" content="text/xml;charset=utf-8" /&gt;&lt;br /&gt;    &lt;title&gt;auto-complete&lt;/title&gt;&lt;br /&gt;    &lt;link rel="stylesheet" type="text/css" href="default.css"/&gt;&lt;br /&gt;    &lt;script type="text/javascript"&gt;&lt;br /&gt;      m_i = -1;&lt;br /&gt;      m_keyval = '';&lt;br /&gt;      &lt;br /&gt;      function showResult(keycode){&lt;br /&gt;        oresult = document.getElementById('result');&lt;br /&gt;        oresult.style.visibility = 'visible';&lt;br /&gt;        otext = document.getElementById('tel_search');&lt;br /&gt;&lt;br /&gt;        if (keycode =='40'){  // down&lt;br /&gt;          if (m_i &gt; 0){&lt;br /&gt;            cancelHighlight('i'+m_i);&lt;br /&gt;          }&lt;br /&gt;          if (m_i &lt; 5){&lt;br /&gt;            m_i++;&lt;br /&gt;          }&lt;br /&gt;          else&lt;br /&gt;          {&lt;br /&gt;            m_i = 1;&lt;br /&gt;          }&lt;br /&gt;          highlight('i'+m_i);&lt;br /&gt;        }&lt;br /&gt;        if (keycode =='38'){  // up&lt;br /&gt;          if (m_i &gt; 0 || m_i &lt; 6){&lt;br /&gt;            cancelHighlight('i'+m_i);&lt;br /&gt;          }&lt;br /&gt;          m_i--;&lt;br /&gt;          highlight('i'+m_i);&lt;br /&gt;        }&lt;br /&gt;        if (keycode =='27' || keycode == '39'){   // escape&lt;br /&gt;          if (m_keyval != ''){&lt;br /&gt;            otext.value = m_keyval;&lt;br /&gt;            m_keyval = '';&lt;br /&gt;          }&lt;br /&gt;          hideResult();&lt;br /&gt;          cancelHighlight('i'+m_i);&lt;br /&gt;          m_i = 0;&lt;br /&gt;        }&lt;br /&gt;        if (keycode =='13'){    // enter&lt;br /&gt;          itemText = getItem('i'+m_i);&lt;br /&gt;          otext.value = itemText;&lt;br /&gt;          m_keyval = itemText;&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;      function setTextValue(i){        &lt;br /&gt;        otext = document.getElementById('tel_search');&lt;br /&gt;        itemText = getItem('i'+i);&lt;br /&gt;        otext.value = itemText;&lt;br /&gt;        m_keyval = itemText;&lt;br /&gt;        m_i = i;&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      function hideResult(){&lt;br /&gt;        oresult = document.getElementById('result');&lt;br /&gt;        oresult.style.visibility = 'hidden';&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      function getItem(id){&lt;br /&gt;        oItem = document.getElementById(id);&lt;br /&gt;        return oItem.innerHTML;&lt;br /&gt;      }&lt;br /&gt;            &lt;br /&gt;      function highlight(id){&lt;br /&gt;        oItem = document.getElementById(id);&lt;br /&gt;        oItem.className = 'highlight';&lt;br /&gt;        oItem.style.backgroundColor = '#789';&lt;br /&gt;        oItem.style.color = '#ee8';&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      function cancelHighlight(id){&lt;br /&gt;        oItem = document.getElementById(id);&lt;br /&gt;        oItem.style.backgroundColor = '#de8';&lt;br /&gt;        oItem.style.color = '#bbb';&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      function mHighlight(i){&lt;br /&gt;        if (m_i &gt; 0){&lt;br /&gt;          cancelHighlight('i'+m_i);&lt;br /&gt;        }&lt;br /&gt;        highlight('i'+i);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      function handleKeyword(keycode){&lt;br /&gt;        if (!(m_i &lt; 0 &amp;&amp; keycode == 40) &amp;&amp; keycode != 9 &amp;&amp; keycode != 37 &amp;&amp; !(m_i &lt; 1 &amp;&amp; keycode == 38) ) {&lt;br /&gt;          showResult(keycode);&lt;br /&gt;          if (m_i == -1) m_i = 0;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;          hideResult();&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    &lt;/script&gt;&lt;br /&gt;  &lt;/head&gt;&lt;br /&gt;  &lt;body onclick="hideResult();"&gt;&lt;br /&gt;  &lt;div id="search"&gt;&lt;br /&gt;    &lt;input type="text" id="tel_search" name="tel_search" onkeyup="handleKeyword(event.keyCode);"/&gt;&lt;br /&gt;    &lt;p&gt;dhtml is fun &lt;/p&gt;&lt;br /&gt;    &lt;div id="result"&gt;&lt;br /&gt;      &lt;ul&gt;&lt;br /&gt;        &lt;li id="i1" onmouseover="mHighlight(1);" onmouseout="cancelHighlight('i1');" onclick="setTextValue(1)"&gt;1erter&lt;/li&gt;&lt;br /&gt;        &lt;li id="i2" onmouseover="mHighlight(2);" onmouseout="cancelHighlight('i2');" onclick="setTextValue(2)"&gt;2tttye&lt;/li&gt;&lt;br /&gt;        &lt;li id="i3" onmouseover="mHighlight(3);" onmouseout="cancelHighlight('i3');" onclick="setTextValue(3)"&gt;3erter&lt;/li&gt;&lt;br /&gt;        &lt;li id="i4" onmouseover="mHighlight(4);" onmouseout="cancelHighlight('i4');" onclick="setTextValue(4)"&gt;4tttye&lt;/li&gt;&lt;br /&gt;        &lt;li id="i5" onmouseover="mHighlight(5);" onmouseout="cancelHighlight('i5');" onclick="setTextValue(5)"&gt;5erter&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/ul&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;  &lt;/div&gt;&lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: default.css&lt;br /&gt;&lt;code&gt;&lt;br /&gt;body {background-color: #e34;}&lt;br /&gt;#search {background-color: #478; height: 4em;position: relative;}&lt;br /&gt;#result {background-color: transparent; max-height: 8em; position: absolute;  overflow: hidden; z-index: 500;left:0; top:1.4em; visibility:hidden; width: 10.2em; padding:0; margin:0;}&lt;br /&gt;#result ul {background-color: #9de; list-style-type: none; padding:0; margin: 0}&lt;br /&gt;  #result li {background-color:#de8; color: #bbb; padding:0; margin: 0.2em}&lt;br /&gt;    .highlight {background-color: #9ab; cursor: pointer}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Jan 2008 20:27:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4990</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
