Learn Flexbox

jQuery Tutorial (w3schools)

jQuery greatly simplifies JavaScript programming.

jQuery is easy to learn.

lesson 1

jquery image

jQuery Introduction

jQuery is a JavaScript Library.

lesson 2

jquery image

What You Should Already Know

HTML, CSS, JavaScript,

Lesson 3

jquery image

The jQuery library contains the following features:

  • - HTML/DOM manipulation
  • - CSS manipulation
  • - HTML event methods
  • - Effects and animations
  • - AJAX
  • - Utilities

Lesson 4

jquery image

Why jQuery?

jQuery has plugins for almost any task out there.

Lesson 5

jquery image

Adding jQuery to Your Web Pages

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

Lesson 6

jquery image

Downloading jQuery and connect

  • - <head>
  • - <script src="jquery-3.5.1.min.js"> </script>
  • - </head>

Lesson 7

jquery image

jQuery CDN and connect

  • - <head>
  • - <script src="https://ajax.google apis.com/ajax/libs/jquery/ 3.5.1/jquery.min.js"> </script>
  • - </head>

Lesson 8

jquery image

jQuery Syntax

Basic syntax is:

  • $(selector).action()
  • - 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"

Lesson 9

jquery image

The Document Ready Event

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

  • - $(document).ready(function(){
  • - // jQuery methods go here...
  • - });

Lesson 10

jquery image

jQuery Selectors

jQuery selectors allow you to select and manipulate HTML element(s).

All selectors in jQuery start with the dollar sign and parentheses:

  • - $( )

Lesson 11

jquery image

The element Selector

The jQuery element selector selects elements based on the element name.

You can select all <p> elements on a page like this:

  • - $("p")

Lesson 12

jquery image

The element Selector Example

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

  • - $(document).ready(function(){
  • - $("button").click(function(){
  • - $("p").hide();
  • - });
  • - });
  • Paragraph Text

Lesson 13

jquery image

The #id Selector

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

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

Lesson 14

jquery image

The .class Selector

The jQuery .class selector finds elements with a specific class.

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

Lesson 15

jquery image

jQuery Syntax For Event Methods

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

Lesson 16

jquery image

jQuery Event Example

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

  • - $(document).ready(function(){
  • - $("p").click(function(){
  • - $(this).hide();
  • - });
  • - });
  • I am paragraph ,If you click on me, I will disappear.

Lesson 17

jquery image

The dblclick() method

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

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

Lesson 18

jquery image

The mouseenter() method

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

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

Lesson 19

jquery image

The mouseleave() method

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

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

Finish level 1

jquery image

Lesson 20

jquery image

The mousedown() method

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!");
  • - });

Lesson 21

jquery image

The mouseup() method

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!");
  • - });

Lesson 22

jquery image

The hover() method

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:

  • - $("#p1").hover(function(){
  • - alert("You entered p1!");
  • - },
  • - function(){
  • - alert("Bye! You now leave p1!");
  • - });

Lesson 23

jquery image

The focus() method

The function is executed when the form field gets focus:

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

Lesson 24

jquery image

The blur() method

The function is executed when the form field loses focus:

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

Lesson 25

jquery image

The on() method

The on() method attaches one or more event handlers for the selected elements. Attach <a> click event to a

element:

  • - $("p").on("click", function(){
  • - $(this).hide();
  • - });

Lesson 26

jquery image

The on() - example

  • - $(document).ready(function(){
  • - $("p").on({ mouseenter: function(){
  • - $(this).css("background-color", "lightgray");},
  • - mouseleave: function(){
  • - $(this).css("background-color", "lightblue");},
  • - click: function(){
  • - $(this).css("background-color", "yellow");} }); });
  • Click or move the mouse pointer.

Lesson 27

jquery image

jQuery hide() and show()

With jQuery, you can hide and show HTML elements with the hide() and show() methods:

  • - $("#hide").click(function(){
  • - $("p").hide();
  • - });
  • - $("#show").click(function(){
  • - $("p").show();
  • - });

Lesson 28

jquery image

jQuery hide() and show() Syntax

The optional speed parameters:"slow", "fast", or milliseconds.

  • - $(selector).hide(speed, callback);
  • - $(selector).show(speed, callback);
  • - $("button").click(function(){
  • - $("p").hide(1000);
  • - });

Lesson 29

jquery image

jQuery toggle()

You can also toggle between hiding and showing an element with the toggle() method.

  • - $("button").click(function(){
  • - $("p").toggle();
  • - });

Syntax:

  • $(selector).toggle(speed, callback);

Lesson 30

jquery image

jQuery Fading Methods

jQuery has the following fade methods:

  • - fadeIn()
  • - fadeOut()
  • - fadeToggle()
  • - fadeTo()

Lesson 31

jquery image

jQuery fadeIn() Method

The jQuery fadeIn() method is used to fade in a hidden element.

Syntax:

  • - $(selector).fadeIn(speed, callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.

The optional callback parameter is a function to be executed after the fading completes.

Lesson 32

jquery image

The fadeIn() example - different parameters:

  • - $(document).ready(function(){
  • - $("button").click(function(){
  • - $("#div1").fadeIn();
  • - $("#div2").fadeIn("slow");
  • - $("#div3").fadeIn(3000);
  • - });
  • - });

Lesson 33

jquery image

jQuery fadeOut() Method

The jQuery fadeOut() method is used to fade out a visible element.

Syntax:

  • $(selector).fadeOut(speed, callback);
  • - $(document).ready(function(){
  • - $("button").click(function(){
  • - $("#div1").fadeOut();
  • - $("#div2").fadeOut("slow");
  • - $("#div3").fadeOut(3000);
  • - });
  • - });

Lesson 34

jquery image

jQuery fadeToggle() Method

The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.

Syntax:

  • $(selector).fadeToggle(speed, callback);
  • $(document).ready(function(){
  • - $("button").click(function(){
  • - $("#div1").fadeToggle();
  • - $("#div2").fadeToggle("slow");
  • - $("#div3").fadeToggle(3000);
  • - });
  • - });

Lesson 35

jquery image

jQuery fadeTo() Method

The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).

Syntax:

  • $(selector).fadeTo(speed,opacity, callback);
  • $(document).ready(function(){
  • - $("button").click(function(){
  • - $("#div1").fadeTo("slow", 0.4);
  • - $("#div2").fadeTo("slow", 0.7);
  • - $("#div3").fadeTo("slow", 0.15);
  • - });
  • - });


lesson 36

jquery image

jQuery Sliding Methods

With jQuery you can create a sliding effect on elements.

jQuery has the following slide methods:

  • slideDown()
  • slideUp()
  • slideToggle()

lesson 37

jquery image

jQuery slideDown() Method

The jQuery slideDown() method is used to slide down an element.

Syntax:

  • $(selector).slideDown(speed,callback);
  • $(document).ready(function(){
  • $("#flip").click(function(){
  • $("#panel").slideDown("slow");
  • });
  • });
  • I am div - Click to slide down panel
  • I am div - Hello world!

lesson 38

jquery image

jQuery slideUp() Method

The jQuery slideUp() method is used to slide up an element.

Syntax:

  • $(selector).slideUp(speed,callback);
  • $(document).ready(function(){
  • $("#flip").click(function(){
  • $("#panel").slideUp("slow");
  • });
  • });
  • I am div - Click to slide up panel
  • I am div - Hello world!

Finish level 2

jquery image

lesson 39

jquery image

jQuery slideToggle() Method

The jQuery slideToggle() method toggles between the slideDown() and slideUp() methods.

Syntax:

  • $(selector).slideToggle( speed, callback);
  • $(document).ready(function(){
  • $("#flip").click(function(){
  • $("#panel").slideToggle("slow");
  • });
  • });
  • I am div - Click to slide the panel down or up
  • I am div - Hello world!

lesson 40

jquery image

jQuery Animations - The animate() Method

The jQuery animate() method is used to create custom animations.

Syntax:

  • $(selector).animate({params}, speed, callback);
  • $(document).ready(function(){
  • $("button").click(function(){
  • $("div").animate({left: '250px'});
  • });
  • });

lesson 41

jquery image

jQuery animate()

By default, all HTML elements have a static position, and cannot be moved.

To manipulate the position, remember to first set the CSS position property of the element to relative, fixed, or absolute!

lesson 42

jquery image

jQuery animate() - Using Relative Values

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("div").animate({
  • left: '250px',
  • height: '+=150px',
  • width: '+=150px'
  • });
  • });
  • });

lesson 43

jquery image

jQuery animate() - Using Pre-defined Values

You can even specify a property's animation value as "show", "hide", or "toggle":

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("div").animate({
  • height: 'toggle',
  • width: 'toggle'
  • });
  • });
  • });

lesson 44

jquery image

jQuery animate() - Uses Queue Functionality

if you want to perform different animations after each other, we take advantage of the queue functionality:

  • $(document).ready(function(){
  • $("button").click(function(){
  • var div = $("div");
  • div.animate({height: '300px', opacity: '0.4'}, "slow");
  • div.animate({width: '300px', opacity: '0.8'}, "slow");
  • div.animate({height: '100px', opacity: '0.4'}, "slow");
  • div.animate({width: '100px', opacity: '0.8'}, "slow");
  • });
  • });

lesson 44

jquery image

jQuery animate() - Uses Queue Functionality - Two

The example below first moves the <div> element to the right, and then increases the font size of the text:

  • $(document).ready(function(){
  • $("button").click(function(){
  • var div = $("div");
  • div.animate({left: '100px'}, "slow");
  • div.animate({fontSize: '3em'}, "slow");
  • });
  • });

lesson 44

jquery image

jQuery stop() Method

The jQuery stop() method is used to stop an animation or effect before it is finished.

The stop() method works for all jQuery effect functions, including sliding, fading and custom animations.

Syntax:

  • $(selector).stop(stopAll,goToEnd);
  • $(document).ready(function(){
  • $("#flip").click(function(){
  • $("#panel").slideDown(5000);
  • });
  • $("#stop").click(function(){
  • $("#panel").stop();
  • });
  • });
  • Button
  • div id="flip"
  • div id="panel"

lesson 45

jquery image

jQuery Callback Functions

JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors.

To prevent this, you can create a callback function.

A callback function is executed after the current effect is finished.

Typical syntax:

  • $(selector).hide(speed,callback);

lesson 46

jquery image

jQuery Callback Functions - Example

The example below has a callback parameter that is a function that will be executed after the hide effect is completed:

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("p").hide("slow", function(){
  • alert("The paragraph is now hidden");
  • });
  • });
  • });

lesson 47

jquery image

jQuery Callback Functions - Example Two

The example below has no callback parameter, and the alert box will be displayed before the hide effect is completed:

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("p").hide(1000);
  • alert("The paragraph is now hidden");
  • });
  • });

lesson 48

jquery image

jQuery Method Chaining

However, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element(s).

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("#p1").css("color", "red") .slideUp(2000) .slideDown(2000);
  • });
  • });

lesson 49

jquery image

jQuery Method Chaining - Example

We could also have added more method calls if needed.

When chaining, the line of code could become quite long. However, jQuery is not very strict on the syntax; you can format it like you want, including line breaks and indentations.

  • $(document).ready(function(){
  • $("button").click(function(){
  • $("#p1").css("color", "red")
  • .slideUp(2000)
  • .slideDown(2000);
  • });
  • });

lesson 50

jquery image

jQuery DOM Manipulation

Three simple, but useful, jQuery methods for DOM manipulation are:

  • - text() - Sets or returns the text content of selected elements
  • - html() - Sets or returns the content of selected elements (including HTML markup)
  • - val() - Sets or returns the value of form fields

lesson 51

jquery image

jQuery DOM Manipulation - Example

Three simple, but useful, jQuery methods for DOM manipulation are:

  • - $(document).ready(function(){
  • - $("#btn1").click(function(){
  • - alert("Text: " + $("#test").text());
  • - });
  • - $("#btn2").click(function(){
  • - alert("HTML: " + $("#test").html());
  • - });
  • - });

lesson 52

jquery image

jQuery DOM Manipulation - Example two

The following example demonstrates how to get the value of an input field with the jQuery val() method:

  • - $(document).ready(function(){
  • - $("button").click(function(){
  • - alert("Value: " + $("#test").val());
  • - });
  • - });

lesson 53

jquery image

Get Attributes - attr()

The jQuery attr() method is used to get attribute values.

The following example demonstrates how to get the value of the href attribute in a link:

  • - $(document).ready(function(){
  • - $("button").click(function(){
  • - alert($("#w3s").attr("href"));
  • - });
  • - });

lesson 54

jquery image

Set Content - text(), html(), and val()

  • - $(document).ready(function(){
  • - $("#btn1").click(function(){
  • - $("#test1").text("Hello world!");
  • - });
  • - $("#btn2").click(function(){
  • - $("#test2").html("<b>Hello world!</b>");
  • - });
  • - $("#btn3").click(function(){
  • - $("#test3").val("Dolly Duck");
  • - });
  • - });

lesson 55

jquery image

A Callback Function for text(), html(), and val()

The following example demonstrates text() and html() with a callback function:

  • $(document).ready(function(){
  • $("#btn1").click(function(){
  • $("#test1").text(function(i, origText){
  • return "Old text: " + origText + " New text: Hello world! (index: " + i + ")";
  • });
  • });
  • $("#btn2").click(function(){
  • $("#test2").html(function(i, origText){
  • return "Old html: " + origText + " New html: Hello world! (index: " + i + ")";
  • });
  • });
  • });

lesson 56

jquery image

Set Attributes - attr()

The jQuery attr() method is also used to set/change attribute values.

The following example demonstrates how to change (set) the value of the href attribute in a link:

  • $("button").click(function(){
  • $("#w3s").attr("href", "https: //www.w3schools.com/ jquery/");
  • });

The attr() method also allows you to set multiple attributes at the same time.

The following example demonstrates how to set both the href and title attributes at the same time:

  • $("button").click(function(){
  • $("#w3s").attr({
  • "href" : "https: //www.w3schools.com/ jquery/",
  • "title" : "W3Schools jQuery Tutorial"
  • });
  • });

lesson 57

jquery image

A Callback Function for attr()

The jQuery method attr(), also comes with a callback function. The callback function has two parameters: the index of the current element in the list of elements selected and the original (old) attribute value. You then return the string you wish to use as the new attribute value from the function.

The following example demonstrates attr() with a callback function:

  • $("button").click(function(){
  • $("#w3s").attr("href", function(i, origValue){
  • return origValue + "/jquery/";
  • });
  • });

lesson 58

jquery image

Add New HTML Content

We will look at four jQuery methods that are used to add new content:

  • - append() - Inserts content at the end of the selected elements
  • - prepend() - Inserts content at the beginning of the selected elements
  • - after() - Inserts content after the selected elements
  • - before() - Inserts content before the selected elements

jQuery append() Method

The jQuery append() method inserts content AT THE END of the selected HTML elements.

  • $("p").append("Some appended text.");

lesson 59

jquery image

jQuery prepend() Method

The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.

  • - $("p").prepend("Some prepended text.");

Finish level 3

jquery image

lesson 60

jquery image

Add Several New Elements With append() and prepend()

The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.

  • function appendText() {
  • var txt1 = "<p>Text.</p>"; // Create text with HTML
  • var txt2 = $("<p> </p>").text("Text."); // Create text with jQuery
  • var txt3 = document.createElement("p");
  • txt3.innerHTML = "Text."; // Create text with DOM
  • $("body").append(txt1, txt2, txt3); // Append new elements
  • }
  • <button>onclick="appendText()"> Append text</button>

lesson 61

jquery image

jQuery after() and before() Methods

The jQuery after() method inserts content AFTER the selected HTML elements.

The jQuery before() method inserts content BEFORE the selected HTML elements.

  • $("img").after("Some text after");
  • $("img").before("Some text before");

lesson 62

jquery image

Add Several New Elements With after() and before()

  • function afterText() {
  • var txt1 = "<b>I </b>"; // Create element with HTML
  • var txt2 = $("<i> </i>").text("love "); // Create with jQuery
  • var txt3 = document.createElement("b"); // Create with DOM
  • txt3.innerHTML = "jQuery!";
  • $("img").after(txt1, txt2, txt3); // Insert new elements after img
  • }

lesson 63

jquery image

To remove elements and content, there are mainly two jQuery methods:

  • remove() - Removes the selected element (and its child elements)
  • empty() - Removes the child elements from the selected element

jQuery remove() Method

The jQuery remove() method removes the selected element(s) and its child elements.

  • $("#div1").remove();

jQuery empty() Method

The jQuery empty() method removes the child elements of the selected element(s).

  • $("#div1").empty();

lesson 64

jquery image

Filter the Elements to be Removed

  • The jQuery remove() method also accepts one parameter, which allows you to filter the elements to be removed.
  • The parameter can be any of the jQuery selector syntaxes.
  • The following example removes all <p> elements with class="test":
  • $("p").remove(".test");

This example removes all <p> elements with class="test" and class="demo":

  • $("p").remove(".test, .demo");

lesson 65

jquery image

jQuery Manipulating CSS

jQuery has several methods for CSS manipulation. We will look at the following methods:

  • addClass() - Adds one or more classes to the selected elements
  • removeClass() - Removes one or more classes from the selected elements
  • toggleClass() - Toggles between adding/removing classes from the selected elements
  • css() - Sets or returns the style attribute

Example Stylesheet

The following stylesheet will be used for all the examples on this page:

  • .important {
  • font-weight: bold;
  • font-size: xx-large;
  • }
  • .blue {
  • color: blue;
  • }

lesson 66

jquery image

jQuery addClass() Method

The following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:

  • $("button").click(function(){
  • $("h1, h2, p").addClass("blue");
  • $("div").addClass("important");
  • });

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

  • $("button").click(function(){
  • $("#div1").addClass("important blue");
  • });

lesson 67

jquery image

jQuery removeClass() Method

The following example shows how to remove a specific class attribute from different elements:

  • $("button").click(function(){
  • $("h1, h2, p").removeClass("blue");
  • });

jQuery toggleClass() Method

The following example will show how to use the jQuery toggleClass() method. This method toggles between

adding/removing classes from the selected elements:

  • $("button").click(function(){
  • $("h1, h2, p").toggleClass("blue");
  • });

lesson 68

jquery image

jQuery css() Method

The css() method sets or returns one or more style properties for the selected elements.

Return a CSS Property

To return the value of a specified CSS property, use the following syntax:

  • css("propertyname");

The following example will return the background-color value of the FIRST matched element:

  • $("p").css("background-color");

Set a CSS Property

To set a specified CSS property, use the following syntax:

  • css("propertyname","value");
  • The following example will set the background-color value for ALL matched elements:
  • $("p").css("background-color", "yellow");

lesson 69

jquery image

Set Multiple CSS Properties

To set multiple CSS properties, use the following syntax:

  • css({"propertyname":"value", "propertyname": "value",...});

The following example will set a background-color and a font-size for ALL matched elements:

  • $("p").css({"background-color": "yellow", "font-size": "200%"});

lesson 71

jquery image

jQuery Dimension Methods

With jQuery, it is easy to work with the dimensions of elements and browser window.

jQuery has several important methods for working with dimensions:

    width()

    height()

    innerWidth()

    innerHeight()

    outerWidth()

    outerHeight()

jQuery width() and height() Methods

The width() method sets or returns the width of an element (excludes padding, border and margin).

The height() method sets or returns the height of an element (excludes padding, border and margin).

The following example returns the width and height of a specified <div> element:

  • $("button").click(function(){
  • var txt = "";
  • txt += "Width: " + $("#div1").width() + "<br>";
  • txt += "Height: " + $("#div1").height();
  • $("#div1").html(txt);
  • });

lesson 72

jquery image

jQuery innerWidth() and innerHeight() Methods

The innerWidth() method returns the width of an element (includes padding).

The innerHeight() method returns the height of an element (includes padding).

The following example returns the inner-width/height of a specified <div> element:

    $("button").click(function(){

    var txt = "";

    txt += "Inner width: " + $("#div1").innerWidth() + "<br>";

    txt += "Inner height: " + $("#div1").innerHeight();

    $("#div1").html(txt);

    });

jQuery outerWidth() and outerHeight() Methods

The outerWidth() method returns the width of an element (includes padding and border).

The outerHeight() method returns the height of an element (includes padding and border).

The following example returns the outer-width/height of a specified <div> element:<div> element:

  • $("button").click(function(){
  • var txt = "";
  • txt += "Outer width: " + $("#div1").outerWidth() + "<br>";
  • txt += "Outer height: " + $("#div1").outerHeight();
  • $("#div1").html(txt);
  • });

lesson 73

jquery image

jQuery innerWidth() and innerHeight() Methods

The outerWidth(true) method returns the width of an element (includes padding, border, and margin).

The outerHeight(true) method returns the height of an element (includes padding, border, and margin).

    $("button").click(function(){

    var txt = "";

    txt += "Outer width (+margin): " + $("#div1").outerWidth(true) + "
    ";

    txt += "Outer height (+margin): " + $("#div1").outerHeight(true);

    $("#div1").html(txt);

    });

jQuery More width() and height()

The following example returns the width and height of the document (the HTML document) and window (the browser viewport):

  • $("button").click(function(){
  • var txt = "";
  • txt += "Document width/height: " + $(document).width();
  • txt += "x" + $(document).height() + "\n";
  • txt += "Window width/height: " + $(window).width();
  • txt += "x" + $(window).height();
  • alert(txt);
  • });

The following example sets the width and height of a specified <div> element:

  • $("button").click(function(){
  • $("#div1").width(500).height(500);
  • });

lesson 74

jquery image

jQuery innerWidth() and innerHeight() Methods

The outerWidth(true) method returns the width of an element (includes padding, border, and margin).

The outerHeight(true) method returns the height of an element (includes padding, border, and margin).

    $("button").click(function(){

    var txt = "";

    txt += "Outer width (+margin): " + $("#div1").outerWidth(true) + "
    ";

    txt += "Outer height (+margin): " + $("#div1").outerHeight(true);

    $("#div1").html(txt);

    });

jQuery More width() and height()

The following example returns the width and height of the document (the HTML document) and window (the browser viewport):

  • $("button").click(function(){
  • var txt = "";
  • txt += "Document width/height: " + $(document).width();
  • txt += "x" + $(document).height() + "\n";
  • txt += "Window width/height: " + $(window).width();
  • txt += "x" + $(window).height();
  • alert(txt);
  • });

The following example sets the width and height of a specified <div> element:

  • $("button").click(function(){
  • $("#div1").width(500).height(500);
  • });

lesson 76

jquery image

jQuery Traversing - Ancestors

With jQuery you can traverse up the DOM tree to find ancestors of an element.

An ancestor is a parent, grandparent, great-grandparent, and so on.

Traversing Up the DOM Tree

Three useful jQuery methods for traversing up the DOM tree are:

    parent()

    parents()

    parentsUntil()

jQuery parent() Method

The parent() method returns the direct parent element of the selected element.

This method only traverse a single level up the DOM tree.

The following example returns the direct parent element of each <span> elements:

  • $(document).ready(function(){
  • $("span").parent();
  • });

jQuery parents() Method

The parents() method returns all ancestor elements of the selected element, all the way up to the document's root element (<html>).The following example returns all ancestors of all <span> elements:

  • $(document).ready(function(){
  • $("span").parents();
  • });

Finish

jquery image