Book Image

HTML5 Data and Services Cookbook

Book Image

HTML5 Data and Services Cookbook

Overview of this book

HTML5 is everywhere. From PCs to tablets to smartphones and even TVs, the web is the most ubiquitous application platform and information medium bar. Its becoming a first class citizen in established operating systems such as Microsoft Windows 8 as well as the primary platform of new operating systems such as Google Chrome OS. "HTML5 Data and Services Cookbook" contains over 100 recipes explaining how to utilize modern features and techniques when building websites or web applications. This book will help you to explore the full power of HTML5 - from number rounding to advanced graphics to real-time data binding. "HTML5 Data and Services Cookbook" starts with the display of text and related data. Then you will be guided through graphs and animated visualizations followed by input and input controls. Data serialization, validation and communication with the server as well as modern frameworks with advanced features like automatic data binding and server communication will also be covered in detail.This book covers a fast track into new libraries and features that are part of HTML5!
Table of Contents (21 chapters)
HTML5 Data and Services Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Autoupdating fields


These days, it is common to have an autoupdate on fields where one section is either the result of given choices or it displays a given image or text block. One example of this is having a password strength calculation; for example, searching for "currency converter" on Google will result in a box where you can do currency conversion between USD and EUR. Linking fields in this way makes sense when we have two or more that are logically linked, or when one is a result form of the other.

To demonstrate this, we will create a converter for temperature where updating one of the fields will result in changes in the other, as the values are linked.

Getting ready

For this recipe, we only need a basic knowledge of jQuery and a simple formula to convert the temperatures between Celsius and Fahrenheit and vice versa:

Celsius = (Fahrenheit -32) x (5/9)

Or:

Fahrenheit = Celsius  x(9/5) +32

How to do it...

  1. First, we are going to create the HTML part and create two input fields that will get autoupdated and add the appropriate labels:

    <div>
    <label for='celsius'>C&deg;</label>
    <input id='celsius' type='text' /> =
    <label for='fahrenheit'>F&deg;</label>
    <input id='fahrenheit' type='text' />
    </div>
  2. Afterwards, we have to make sure that we have included jQuery:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script>
  3. Following this, we can add the script that will handle the binding between the fields:

    $(document).ready(function() {
      $('#celsius').keyup(function(data) {
      var celsius = new Number(data.currentTarget.value);
      var farenheit =celsius *(9/5) + 32;
        $('#farenheit').val(farenheit);
        });
       $('#farenheit').keyup(function(data) {
           var farenheit = new Number(data.currentTarget.value);
        var celsius =(farenheit-32)*(5/9);
         $('#celsius').val(celsius);
         });
            });

This will connect and automatically calculate the temperature back and forward.

How it works…

Let's first take a look at the display part where there is nothing specific; here we use a simple input type text and add the appropriate labels for each field. Furthermore, we can use the escaped character &deg; that will show the degree character.

If we take a look at the jQuery keyup event, we can see that it's executed when a user releases a key on the keyboard on a given element. This event can be attached on any HTML element, but it will only work when the element is in focus; so it mostly makes sense to use it on input elements. As the keyup event has an option to execute a function that will accept the event object, so for our case, it is as follows:

$('#celsius').keyup(function(event) {

In the event object, we can access the element that fired the event and access its value:

event.currentTarget.value

After that, we can do the calculation (celsius *(9/5) + 32) and set the result as a value to the other element that displays it in Fahrenheit:

$('#fahrenheit').val(fahrenheit);

As we wanted the binding to work both ways, we can do the same on the input field for Fahrenheit:

$('#farenheit').keyup(function(event) {

And of course, you need to use the appropriate formula (fahrenheit-32)*(5/9)) for returning back to Celsius.

There's more...

While this recipe shows a simple use of jQuery event to make an instant update on input text, it can also be applied for creating autocomplete boxes or features, such as Google's instant search. The idea here is that we can and should use one- or two-way binding for various HTML elements, especially when we are talking about derived data or data that is a representation of the same source.