Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
HTML5 Web Application Development By Example : Beginner's guide

You're reading from   HTML5 Web Application Development By Example : Beginner's guide Learn how to write rich, interactive web applications using HTML5 and CSS3 through real-world examples. In a world of proliferating platforms and devices, being able to create your own ‚Äúgo-anywhere‚Äù applications gives you a significant advantage.

Arrow left icon
Product type Paperback
Published in Jun 2013
Publisher Packt
ISBN-13 9781849695947
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jody Gustafson Jody Gustafson
Author Profile Icon Jody Gustafson
Jody Gustafson
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. The Task at Hand FREE CHAPTER 2. Let's Get Stylish 3. The Devil is in the Details 4. A Blank Canvas 5. Not So Blank Canvas 6. Piano Man 7. Piano Hero 8. A Change in the Weather 9. Web Workers Unite 10. Releasing an App into the Wild A. Pop Quiz Answers Index

Time for action – editing a task in the list

So far we have a tasklist that we can add tasks to, remove tasks from, and change the order of the tasks. Let's add some functionality to allow the user to change the name of a task. When the user clicks on a task name we will change it to a text input field. To do that we need to add a text input field to our task element template right after the task name:

<li class="task">
    <div class="tools">
        <button class="delete" title="Delete">X</button>
        <button class="move-up" title="Up">^</button>
        <button class="move-down" title="Down">v</button>
    </div>
    <span class="task-name"></span>
    <input type="text" class="task-name hidden"/>
</li>

We give it a class of task-name to identify it, and also add the hidden class so it's not visible by default. We only want to show it when the user clicks on the task name. So let's go into the JavaScript file and add an event handler on the <span> element to the end of our addTaskElement() method:

$("span.task-name", $task).click(function() {
    onEditTaskName($(this));
});

Let's break this down. First we get the span with the class of task-name that is a child of the task element. Then we add a click event handler that calls the onEditTaskName() method. The onEditTaskName() method takes a reference to the <span> element as a parameter. When you are in a jQuery event handler function, this refers to the element that was the source of the event. So $(this) creates a jQuery object that wraps the <span> element so we can call jQuery methods on it:

function onEditTaskName($span)
{
    $span.hide()
        .siblings("input.task-name")
        .val($span.text())
        .show()
        .focus();
}

Even though the onEditTaskName() method technically contains one line of code, there is a lot going on. It uses function chaining to do a lot of work in a compact statement. First it hides the <span> element. Then it gets the text input field by looking for a sibling of the <span> element, that is, an <input> element with a class of task-name. Then it sets the value of the text field with the task name which it gets from the <span> element using jQuery's text() method. Finally, it makes the text field visible and sets the focus on it.

When the user clicks on the task name, it appears to change into an editable text field right before their eyes. Now all we need is a way to change it back when the user is done editing the name. To do that we'll add a change event handler to the text field, which gets fired when the user changes the text field and hits Enter or leaves it. Add this to the end of the addTaskElement() method:

$("input.task-name", $task).change(function() {
    onChangeTaskName($(this));
});

This works the same way as the task name click event handler. We are going to call a method named onChangeTaskName() and pass it a jQuery object that wraps the text field's input element:

function onChangeTaskName($input)
{
    $input.hide();
    var $span = $input.siblings("span.task-name");
    if ($input.val())
    {
        $span.text($input.val());
    }
    $span.show();
}

First we hide the text input field, and then get the task name <span> element and store it in a variable. Before updating the name we check to make sure that the user actually typed something in. If so, we update the task name. Finally, we call show() to make the task name visible again. The user sees the text field turn back into static text.

There is one last thing left to do. If the user clicks off the field without changing anything, we will not get a change event and the text field will not get hidden. We can get a blur event when this happens though. So let's add a blur event handler to the text field that hides it and shows the static task name <span> element:

$("input.task-name", $task).change(function() {
    onChangeTaskName($(this));
})
.blur(function() {
    $(this).hide().siblings("span.task-name").show();
});

What just happened?

We added a text field to our task template that gets shown when the user clicks on the task name, so they can edit the task name. When the task name text field changes, it updates the task name label.

What just happened?
You have been reading a chapter from
HTML5 Web Application Development By Example : Beginner's guide
Published in: Jun 2013
Publisher: Packt
ISBN-13: 9781849695947
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image