Code Monkey home page Code Monkey logo

jquery's Introduction

Exercises

  1. Refactor your ticker project to use jQuery for all DOM manipulations.

  2. Using jquery, create a modal dialog for your 'Welcome to Berlin and the Future' page. A modal dialog is a popup that users must interact with before they can do anything else. Typically, modals on website involve a box of content sitting on top of a semi-transparent background that fills the browser window.

    Your modal should appear 1 second after users arrive at the page. The modal should disappear when the user clicks the X in the upper right corner.

โ€‹

Modal

Some Theory

jQuery

jQuery is probably the most widely used Javascript library of all time. It's popularity seems to have peaked and entered decline, primarily because many of the problems it solves have become less significant as browsers have improved and become more consistent. However, it is still incredibly widely used and it is very likely you will encounter it on projects in the future.

jQuery provides tons of methods designed to help you traverse the DOM tree, manipulate elements, make ajax requests, and handle user action.

DOM

When you include jQuery, it creates two global variables, jQuery and $, both of which refer to the same thing: a function that returns jQuery objects.

If you pass a selector to this function, it returns an object representing any elements on the page that match the selector. Note that it does not return the elements themselves. The object it returns has references to the elements themselves, but you usually do not need to access them. The jQuery object wraps the elements. When you call methods on the jQuery object, it acts upon the elements without the methods being directly attached to them.

Most jQuery methods return the object they belong to, which allows you to chain your method calls.

$('h1').css({
    color: 'red'
}).html('jQuery is pretty great!');

In this example, the style.color and innerHTML of all <h1> elements of the page have been changed. No loop is required.

If you pass a string containing HTML content to jQuery or $, it creates the element(s).

$('<div>I love jQuery!</div>').css({
    textDecoration: 'underline'
}).addClass('happy').appendTo('body');

If you pass a reference to an element to jQuery or $, a wrapped object is returned.

$(document.body).css('backgroundColor', '#fff');

It is common for jQuery methods that are setters (such as attr, val, and html) to also be getters. If they are not passed a value to set, they return the current value.

Events

jQuery has had several different APIs for adding and removing event listeners through the years (bind/unbind, live/die, delegate/undelegate). The current methods to use for this are on and off.

$('.pretty').on('mousedown', function fn(e) {
    console.log(e.currentTarget.id);
    $(e.currentTarget).off('mousedown', fn);
});

jQuery allows you to give events a namespace, which is very handy for removing listeners without references to the functions themselves.

$('a').on('mouseover.removeAfterFirstClick', function() {
    console.log('mouseover!');
}).on('click.removeAfterFirstClick', function(e) {
    $(e.currentTarget).off('.removeAfterFirstClick'); //removes all events in the removeAfterFirstClick namespace
});

You can give events arbitrary names and trigger them at will.

$('body').on('whatever', function() {
    console.log('The "whatever" event happened');
}).trigger('whatever');

jQuery also supports creating custom events that are composites of other events.

Including jQuery

In order to use jquery, you have to include it with a script tag in your HTML. You can either download the latest version or include it from the CDN like so:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

jquery's People

Contributors

doctor-uz avatar

Watchers

James Cloos 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.