Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active March 28, 2018 13:40
Show Gist options
  • Save amelieykw/7d85aca4da93b01d1eb702ca2cbb833b to your computer and use it in GitHub Desktop.
Save amelieykw/7d85aca4da93b01d1eb702ca2cbb833b to your computer and use it in GitHub Desktop.
[jQuery tutorial - w3school] #jQuery #tutorial #w3school

There are several ways to start using jQuery on your web site. You can:

  • Download the jQuery library from jQuery.com
  • Include jQuery from a CDN, like Google

jQuery CDN

If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).

Both Google and Microsoft host jQuery.

To use jQuery from Google or Microsoft, use one of the following:

Google CDN

<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

Microsoft CDN

<head>
	<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>

With jQuery you select (query) HTML elements and perform "actions" on them.

Basic syntax is: $(selector).action()

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

Examples:

  • $(this).hide() - hides the current element.
  • $("p").hide() - hides all <p> elements.
  • $(".test").hide() - hides all elements with class="test".
  • $("#test").hide() - hides the element with id="test".

all jQuery methods in our examples, are inside a document ready event:

$(document).ready(function(){

   // jQuery methods go here...

});

This is to prevent any jQuery code from running before the document is finished loading (is ready).

an even shorter method for the document ready event:

$(function(){

   // jQuery methods go here...

});

Best Practice :

  • wait for the document to be fully loaded and ready before working with it
  • have your JavaScript code before the body of your document, in the head section.

Bad Action Results :

some examples of actions that can fail if methods are run before the document is fully loaded:

  • Trying to hide an element that is not created yet
  • Trying to get the size of an image that is not loaded yet

jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more.

All selectors in jQuery start with the dollar sign and parentheses: $().

The element Selector

selects elements based on the element name

select all <p> elements on a page:

$("p")

Example

When a user clicks on a button, all <p> elements will be hidden:

$(document).ready(function(){
    $("button").click(function(){
        $("p").hide();
    });
});

The #id Selector

uses the id attribute of an HTML tag to find the specific element.

An id should be unique within a page ---> so you should use the #id selector when you want to find a single, unique element.

To find an element with a specific id:

$("#test")

Example

When a user clicks on a button, the element with id="test" will be hidden:

$(document).ready(function(){
    $("button").click(function(){
        $("#test").hide();
    });
});

The .class Selector

finds elements with a specific class

To find elements with a specific class :

$(".test")

Example

When a user clicks on a button, the elements with class="test" will be hidden:

$(document).ready(function(){
    $("button").click(function(){
        $(".test").hide();
    });
});

More Examples of jQuery Selectors

Syntax Description
$("*") Selects all elements
$(this) Selects the current HTML element
$("p.intro") Selects all <p> elements with class="intro"
$("p:first") Selects the first <p> element
$("ul li:first") Selects the first <li> element of the first <ul>
$("ul li:first-child") Selects the first <li> element of every <ul>
$("[href]") Selects all elements with an href attribute
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"
$(":button") Selects all <button> elements and <input> elements of type="button"
$("tr:even") Selects all even <tr> elements
$("tr:odd") Selects all odd <tr> elements

jQuery Selector Reference

Functions In a Separate File

<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	
    <script src="my_jquery_functions.js"></script>
</head>

to respond to events in an HTML page

What are Events?

All the different visitor's actions that a web page can respond to are called events.

An event represents the precise moment when something happens.

Examples:

  • moving a mouse over an element
  • selecting a radio button
  • clicking on an element

The term "fires/fired" is often used with events. (Example: "The keypress event is fired, the moment you press a key".)

some common DOM events:

Mouse Events Keyboard Events Form Events Document/Window Events
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave blur unload

jQuery Syntax For Event Methods

most DOM events have an equivalent jQuery method.

To assign a click event to all paragraphs on a page :

$("p").click();

The next step is to define what should happen when the event fires.

You must pass a function to the event:

$("p").click(function(){
  // action goes here!!
});

Commonly Used jQuery Event Methods

  • $(document).ready()
  • click()
  • dblclick()
  • mouseenter()
  • mouseleave()
  • mousedown()
  • mouseup()
  • hover()
  • focus()
  • blur()
  • on()

$(document).ready()

The $(document).ready() method allows us to execute a function when the document is fully loaded.

click()

The click() method attaches an event handler function to an HTML element.

The function is executed when the user clicks on the HTML element.

The following example says:

When a click event fires on a <p> element; hide the current <p> element:

$("p").click(function(){
    $(this).hide();
});

dblclick()

The function is executed when the user double-clicks on the HTML element:

$("p").dblclick(function(){
    $(this).hide();
});

mouseenter()

The function is executed when the mouse pointer enters the HTML element:

$("#p1").mouseenter(function(){
    alert("You entered p1!");
});

mouseleave()

The function is executed when the mouse pointer leaves the HTML element:

$("#p1").mouseleave(function(){
    alert("Bye! You now leave p1!");
});

mousedown()

The function is executed, when the left, middle or right mouse button is pressed down, while the mouse is over the HTML element:

$("#p1").mousedown(function(){
    alert("Mouse down over p1!");
});

mouseup()

The function is executed, when the left, middle or right mouse button is released, while the mouse is over the HTML element:

$("#p1").mouseup(function(){
    alert("Mouse up over p1!");
});

hover()

The hover() method :

  • takes two functions :
    1. The first function is executed when the mouse enters the HTML element
    2. the second function is executed when the mouse leaves the HTML element
  • = **a combination of the mouseenter() and mouseleave() methods
$("#p1").hover(function(){
    alert("You entered p1!");
},
function(){
    alert("Bye! You now leave p1!");
});

focus()

The focus() method attaches an event handler function to an HTML form field.

The function is executed when the form field gets focus:

$("input").focus(function(){
    $(this).css("background-color", "#cccccc");
});

blur()

The blur() method attaches an event handler function to an HTML form field.

The function is executed when the form field loses focus:

$("input").blur(function(){
    $(this).css("background-color", "#ffffff");
});

on()

The on() method attaches one or more event handlers for the selected elements.

  1. Attach a click event to a <p> element:
$("p").on("click", function(){
    $(this).hide();
});
  1. Attach multiple event handlers to a <p> element:
$("p").on({
    mouseenter: function(){
        $(this).css("background-color", "lightgray");
    }, 
    mouseleave: function(){
        $(this).css("background-color", "lightblue");
    }, 
    click: function(){
        $(this).css("background-color", "yellow");
    } 
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment