• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2026 Replies
  • 331 Replies
    1928 Replies

Autocomplete in HTML5

User avatar
mikethedj4
VIP - Site Partner
Posts: 2593

Autocomplete in HTML5

Mon Jan 20, 2014 8:21 am

Try the demo out - http://jsbin.com/ezANAco/1/edit

Here's something I came across the other day while searching the W3C website while I was looking for an alternative to JQuery UI's Autocomplete feature.

The HTML:

Code: Select all

<input class="font-family" type="text" list="f-f-prop">
<datalist id="f-f-prop">
  <option value="serif"></option>
  <option value="sans"></option>
  <option value="arial"></option>
  <option value="arial black"></option>
  <option value="courier"></option>
  <option value="comic sans ms"></option>
  <option value="helvetica"></option>
  <option value="impact"></option>
  <option value="lucida sans"></option>
  <option value="tahoma"></option>
  <option value="times new roman"></option>
  <option value="verdana"></option>
</datalist>
The JQuery/Javascript:

The say something input is pretty simple we use an on keyup/change event that if the text is equal to whatever we set our function to (which I show in a datalist shown above) it'll call whatever we want within our function after the text value is equal to our function. (hopefully that didn't sound to complicated)

NOTE it's important to remember that change is triggered when the input type is not focused if it is it can not detect a change until focus is off. This is the prime reason why almost all web based text editors use code mirroring methods like Ace, CodeMirror, & Orion. Some popular apps that use these libraries are Google Apps Script, Adobe Edge Code, & the Cloud9IDE

Here's how we detect the text in our textbox and if the text is equal to what we want we then can tell it what to do. In our case we're just going to say hello.

Code: Select all

$('.talk').on('keyup change', function() {
  if($(this).val() === 'Say Hi') {
    alert('Ello Mate!');
  }
});
I also have included another function so you can choose between the fonts listed in the datalist to change the body's font. You can also use another font not listed in the data list if it's installed on your machine, but you can not link to other fonts.

Enjoy!

Here's the full code:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>AutoComplete Example</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<style type='text/css'>
body {
  padding: 2em 0;
  color: #024;
  background: #48f;
}

div { top:0; left:0; }
.talk { width: 100%; border: 0; }

footer, div {
  position:fixed;
  right:0;
  height: auto;
  padding: .3em;
  background: #024;
}

footer {
  bottom:0; left:0;
  text-align: center;
  color: #ccc;
}
</style>
<script type='text/javascript'>
$(document).ready(function() {
  $('.font-family').on('keyup change', function() {
    $('body').css('font-family', $(this).val());
  });
  
  $('.talk').on('keyup change', function() {
    var val = $(this).val();
    
    if(val === 'Say Hi') {
      alert('Ello Mate!');
    }
    
    if(val === 'Say Bye') {
      alert('Bye Bye');
    }
    
    if(val === 'Clear') {
      $(this).val('');
    }
  });
});
</script>
</head>
<body>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.
  
  <p>Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula semper consectetur sagittis, nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum, odio nec pretium volutpat, arcu ante placerat erat, non tristique elit urna et turpis. Quisque mi metus, ornare sit amet fermentum et, tincidunt et orci. Fusce eget orci a orci congue vestibulum. Ut dolor diam, elementum et vestibulum eu, porttitor vel elit. Curabitur venenatis pulvinar tellus gravida ornare. Sed et erat faucibus nunc euismod ultricies ut id justo. Nullam cursus suscipit nisi, et ultrices justo sodales nec. Fusce venenatis facilisis lectus ac semper. Aliquam at massa ipsum. Quisque bibendum purus convallis nulla ultrices ultricies. Nullam aliquam, mi eu aliquam tincidunt, purus velit laoreet tortor, viverra pretium nisi quam vitae mi. Fusce vel volutpat elit. Nam sagittis nisi dui.</p>

  <p>Suspendisse lectus leo, consectetur in tempor sit amet, placerat quis neque. Etiam luctus porttitor lorem, sed suscipit est rutrum non. Curabitur lobortis nisl a enim congue semper. Aenean commodo ultrices imperdiet. Vestibulum ut justo vel sapien venenatis tincidunt. Phasellus eget dolor sit amet ipsum dapibus condimentum vitae quis lectus. Aliquam ut massa in turpis dapibus convallis. Praesent elit lacus, vestibulum at malesuada et, ornare et est. Ut augue nunc, sodales ut euismod non, adipiscing vitae orci. Mauris ut placerat justo. Mauris in ultricies enim. Quisque nec est eleifend nulla ultrices egestas quis ut quam. Donec sollicitudin lectus a mauris pulvinar id aliquam urna cursus. Cras quis ligula sem, vel elementum mi. Phasellus non ullamcorper urna.</p>
  
  <div>
    <input type="text" class="talk" list="talk" placeholder="Say something...">
    <datalist id="talk">
      <option value="Say Hi">
      <option value="Say Bye">
      <option value="Clear">
      </datalist>
  </div>
  
  <footer>
    font-family: 
    <input class="font-family" type="text" list="f-f-prop">
    <datalist id="f-f-prop">
      <option value="serif"></option>
      <option value="sans"></option>
      <option value="arial"></option>
      <option value="arial black"></option>
      <option value="courier"></option>
      <option value="comic sans ms"></option>
      <option value="helvetica"></option>
      <option value="impact"></option>
      <option value="lucida sans"></option>
      <option value="tahoma"></option>
      <option value="times new roman"></option>
      <option value="verdana"></option>
    </datalist>
  </footer>
</body>
</html>
Image

User avatar
Dummy1912
VIP - Donator
Posts: 1998

Re: Autocomplete in HTML5

Mon Jan 20, 2014 10:39 am

:lol: he spoke to me :P :lol:
nice work #mikethedj4

User avatar
mikethedj4
VIP - Site Partner
Posts: 2593

Re: Autocomplete in HTML5

Wed Jan 22, 2014 8:29 pm

Thanks don't forget to +rep
Image

Post Reply

Return to “Tutorials”