Code Monkey home page Code Monkey logo

jquery-and-browser-storage's Introduction

Persisting Form Input with jQuery and Browser Storage

Objectives

After this lesson, students will be able to:

  • Import jQuery to a webpage
  • Construct a jQuery selector
  • Create HTML forms and store user input in the browser
  • Read and write data with localStorage and sessionStorage
  • Understand the pros and cons of using built-in browser storage

Preparation

Before this lesson, students should already be able to:

  • Write basic HTML/CSS
  • Read and write basic vanilla Javascript

Intro to jQuery (15 min)

What is jQuery?

jQuery is a Javascript library with a lot of useful pre-defined functions and expressions. Basically you can think of it as a very large chunk of Javascript code that you add to your webpage before you add your own Javascript, so you can use all these functions and expressions in your own code.

Importing jQuery in index.html

In order to start with jQuery, you need to copy this somewhere on your HTML page, above the script import tag of your own Javascript, like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script src="myjavascript.js"></script>

What is "$"

One of the most important pieces of jQuery is the $ sign. This is an alias for jQuery, and it means "access the jQuery library to do something". Here are a few examples:

$("body").hide(); // Hides the body of your page
$(".link").css("color", "red"); // Turns all links red
$(document).ready(function(){

   // Yay our page loaded!!
   // jQuery methods go here...

});

jQuery Selectors

Typical jQuery syntax has three parts:

  • A $ sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)

jQuery selectors fall into the same categories as the CSS selectors you have already seen. Most importantly:

  • Tag Name (e.g. "p", "h1", "body")
  • Class (e.g. ".default-button", ".col-md-6")
  • ID (e.g. "#catPicture", "#student26")

So what would I write if I wanted to change student26's background color to blue?

Independent Practice (10 min)

Take a minute to think out the following steps. In pairs, complete the following steps:

  • Create a boilerplate index.html page (remember the <h + tab trick in Sublime?)
  • Import jQuery (Hint: use the line above)
  • Create a heading tag (h1, h2, or h3)
  • Give it a class
  • Create an inline-script tag below your jQuery import
  • Use jQuery to select this heading, and change its color

Bonus:

  • If you have time, use jQuery to select another element, and change its positioning or size

Why do we need localStorage?

In this Unit we will be building Browser games with HTML, CSS and client-side JavaScript. Although we are not yet ready to connect a database on the backend or store data on a server, the browser offers the ability to persist data locally using sessionStorage, localStorage and some other options we may explore later in the course.

Creating a game, you may wish to allow your users to store their names and high scores, or keep track of some other simple data.

Here is a look at some documentation for the Web Storage API.

Warm-up (15 min)

Before we begin, let's look at the files in browser-storage-examples. sessionStorage.html is the most basic. We will go through this line-by-line in class. localStorage1.html does the same thing, but it adds the clear button. localStorage2.html takes things a bit farther. Take a look at the code and without opening it in the browser see if you can explain what's happening.

Let's brainstorm some other tasks we can accomplish with localStorage or sessionStorage

Instructions:

  • Divide into groups of 3-4
  • Figure out something to keep track of on a simple single-page web app
  • Create a Form with the tag
  • Save the info to localStorage
  • Feel free to "borrow" liberally from the example code in the repo, but no copy-paste

Remember: localStorage and sessionStorage only store strings, so you may need to parseInt() if you need to do anything with the user data after it has been entered.

Resources:

jQuery Intro on W3Schools

jquery-and-browser-storage's People

Contributors

zebgirouard avatar benhulan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.