Back to top

jQuery

Online Resources

jQuery.com - This link is where you can download the files or find the CDN link for jQuery.

jQuery UI Website

W3Schools: jQuery Tutorial

jQuery Cheat Sheet

Magnific Popup Plugin

Basics

How to connect jQuery to your project

How to Test for jQuery Connection

$ or jQuery

You can start your jQuery commands with either the symbol $ or the word jQuery

Use $ because the point of jQuery is to shorten the code you need.

For example:

Select Elements with jQuery

W3Schools: jQuery Selectors

W3Schools: jQuery Selectors Reference

JavaScript vs. jQuery

jQuery allows you to select one or many elements all at the same time, unlike JavaScript where you had to specifically choose a selector type to get what you wanted.

Manipulate Styles with jQuery

W3Schools: jQuery - Get and Set CSS Classes

It is easy to manipulate the style of elements with jQuery.

There are multiple methods that can be used:

addClass()

Add classes to a single element or multiple elements at one time:

You can specify multiple classes within the addClass() method:

removeClass()

You can remove a specific class attribute from different elements:

toggleClass()

This method toggles between adding/removing classes from the selected elements:

css()

This method sets or returns one or more style properties for the selected elements.

To return the value of a specified CSS property:

To set a specified CSS property:

To set multiple CSS properties:

Example: Click on circle and change background color to purple:

Manipulate Text with jQuery

W3Schools: jQuery text() Method

W3Schools: jQuery html() Method

There are two different ways you can manipulate text using jQuery:

  1. text() method sets or returns the text content of the selected elements
  2. html() method sets or returns the content (innerHTML → text + HTML markup) of the selected elements

text()

html()

Example: Change html after clicking on circle:

- You can change .click to .hover to change the text with mouse hover

Manipulate Attributes with jQuery

W3Schools: jQuery attr() Method

Example: Set the anchor tag:

Example: Change the anchor tag on click:

Add Event Listeners with jQuery

W3Schools: jQuery Event Methods: Full List

W3Schools: jQuery Event Methods: Select Ones With Examples

All the different visitors' actions that a web page can respond to are called events. An event represents the exact moment when something happens. Examples: hover a mouse over an element, select a radio button, click a button, etc. Most DOM events from JavaScript have an equivalent jQuery method

Following are some of the most common events:

click()

This attached an event handler function to the HTML element. The function is executed when the user clicks on the HTML element.

hover()

This method takes two functions and a combination of the mouseenter() and mouseleave() methods.

The first function is executed when the mouse enters the HTML element, and the second function is executed when the mouse leaves the HTML element:

focus() and blur()

Both the focus() method and the blur() method attaches an event handler function to an HTML form field. The focus() function is executed when the form field gets focus. The blur() function is executed when the form field loses focus.

Add Elements with jQuery

W3Schools: jQuery - Add Elements

Add Elements

There are four different ways to add new content using jQuery:

  1. append() - Inserts content at the end of the selected elements
  2. prepend() - Inserts content at the beginning of the selected elements
  3. after()Inserts content after the selected elements
  4. before()Inserts content before the selected elements

Each are done the same way. Here is one example:

Remove Elements with jQuery

W3Schools: jQuery - Remove Elements

Remove Elements

There are two main ways to remove content using jQuery:

  1. remove() - Removes the selected element (and its child elements).
  2. empty() - Removes the child elements from the selected element.

Examples:

remove() - this would remove the entire div and all of it's text.

empty() - this would leave the div, but remove all of it's text.

Website Animations

W3Schools: jQuery Effects - animate()

W3Schools: jQuery Effect Methods - List of all jQuery animation effects

animate()

Syntax:

$(selector).animate({params},speed,callback);

  1. The required params parameter defines the CSS properties to be animated.
  2. The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.
  3. The optional callback parameter is a function to be executed after the animation completes.

Remember that all CSS property names must be camel-case when used with this method. You will need to write paddingLeft instead of padding-left, etc.

Also, you can only animate CSS values that take a numeric value. Example: {opacity: 0.5} If you want to use a percentage instead of number, make it a string. Example: {margin: "20%"}

Also, color animation is not included in the core jQuery library. You can download a plugin for it from jQuery.com if you would like to do that.

Example: Simple fade out (with 1 sec speed) on click:

If multiple elements are on the page within the .square class, then inside the function use this instead of .square. It will only apply click event to what you actually clicked on.

fadeIn(), fadeOut(), fadeToggle(), fadeTo()

The fade() methods can take three different parameters. Speed, easing, and callback.

The speed is in milliseconds, ie - 1000 = 1 second

Example for speed is above under animate.

Example for callback function: Here something else will happen after the fade

AJAX

W3Schools: jQuery AJAX Methods

API.jQuery.com: AJAX

AJAX allows you to:

Use AJAX to get data from another file and use it on your webpage (including fail function)

Regular Expressions

W3Schools: JavaScript Regular Expressions

w3Schools: JavaScript RegEx Reference

A regular expression is a sequence of characters that form a search pattern. It can be a single character or a more complicated pattern. The search pattern can be used for text search and text replace operations.

Example:

/Great/i

This i stands for case insensitive search. It will search for Great and great, and all other variations

/e/g

This g stands for global. It will return not only if it's included, but will also show how many times it is there.

jQuery UI Plugin

jQueryUI.com

Download the stable version from the website above, and save it in the file folder you want to use it in.

Make sure you add these links in this specific order (with examples):

Coding Challenges

Make Item Disappear on Click

Using an event listener on an item with a specific class, make the item disappear on a mouseclick.


  • $(".square").click(function() {
     $(".square").fadeOut(1000);
    });


If there are multiple elements on the page within the same class, then inside of the function use this instead of the class name. It will only apply the click event to what you actually clicked on.


  • $(".square").click(function() {
     $("this").fadeOut(1000);
    });


Click on Div and Receive Alert

Create an alert box to pop up once you click on a div (circle vs square)


  • $("div").click(function() {
     $("this").attr("id") == ("circle"); {
      alert("You clicked on a circle");
     } else {
      alert("You clicked on a square");}
    });