Giter VIP home page Giter VIP logo

jquery-dom's Introduction

The DOM & jQuery

Learning Objectives

  • Explain what the DOM is and how it is structured
  • Select and target DOM elements using jQuery selectors
  • Create, read, update, and delete DOM elements
  • Change the attributes or content of a DOM element
  • Explain the benefits of jQuery
  • Explore jQuery methods for DOM manipulation and traversal

Framing (10 minutes / 0:10)

In the Objects & Functions lesson, you learned about objects as data structures and how we can use them to store data and methods. Today, we will learn about how Javascript uses objects to represent what you see in the browser.

The Document Object Model is a programming interface for HTML. When you load HTML into the browser, it gets converted into an object-based DOM structure. The visual representation of this is what you see when you open up Developer Tools in the browser.

An HTML document is available for us to manipulate as an object, and this object is structured and stored like an upside down tree, like this...

Or this...

html
└── head
│   ├──title
│   ├──meta
│   ├──link[rel="stylesheet"]
|   └──script[type="text/javascript"]
|
└── body
    ├── header
    │   ├── h1
    │   └── nav
    └── section.simplicity
    |   └── h2
    │   └── article
    ├── section.life
    |   └── h2
    │   └── article
    │       └── block_quote
    │       └── block_quote
    └── footer

The Document Object

Each web page loaded in the browser has its own document object. The document interface serves as an entry point to the web page's content. The document is an example of a host object--that is, an object provided to Javascript by the browser environment.

Nodes

Everything in the DOM is a "node." Elements are called "element nodes," attributes are called "attribute nodes," the text inside elements are called "text nodes." There are comment nodes. The document itself is called a document node.

You also can refer to nodes by their relationships to each other. For example, in the graphic above, you would say that the body element is the parent to the two div elements contained inside it, which are called child nodes. The two divs are also siblings to one another because they are on the same level in the tree structure.

Let's look at another example...

jQuery Basics (5 minutes / 0:15)

Understanding the DOM is central to working in Javascript. Javascript uses the DOM to create dynamic HTML. This includes adding new HTML elements and attributes, changing CSS styles in a page and removing existing elements and attributes.

jQuery is a Javascript library that is intended to make it easier to use Javascript on your website. It's known as the "write less, do more" library. One of its main features is that it makes DOM traversal--that is, finding HTML elements based on their relationships with other elements--and DOM manipulation much more simple. Another major benefit is that it enables you to write code that behaves the same across different browsers and browser versions.

After working with CSS, you'll find jQuery to be a friendly way to dive into interactive development because it also emphasizes the use of selecting elements so you can do stuff to them.

It's important to note that jQuery is Javascript. Every jQuery method is, under-the-hood, executing Javascript code. You can see this by looking through the jQuery source code.

Set Up Environment (5 minutes / 0:20)

$ git clone https://github.com/ga-wdi-exercises/jquery-playground.git
$ cd jquery-playground
$ atom .
$ open index.html

You can add jQuery to your site by either downloading it from the jQuery website and hosting it locally or using what's called a Content Delivery Network (CDN). A CDN is a collection of global servers that caches and delivers content including Javascript files.

Today, we'll use a CDN...

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>

We're going to add the following code to the top of the script.js file when using jQuery...

$(document).ready(function(){
  console.log("hello world")
})

Everything placed inside the $(document).ready() method will not be processed until all the HTML has been loaded. In this case, that code is console.log("hello world").

Selectors (5 minutes / 0:25)

jQuery selectors enable you to find and manipulate HTML elements.

Get element by ID

$("#someId")
var red = $("#red")
console.log(red)
Vanilla Javascript syntax
document.getElementById("red")

Get elements by tag name

$("li")

This will return all the tags of that name.

var liTags = $("li")
console.log(liTags)
Vanilla Javascript syntax
document.querySelectorAll("li")

Get elements by class

$(".myClass")

This will return all elements with that class.

var liClass = $(".black")
console.log(liClass)
Vanilla Javascript syntax
document.querySelectorAll(".black")

All valid CSS selectors and pseudo-selectors work

var lastBlackLi = $(".black:last-of-type")
console.log(lastBlackLi)

Traversal Methods (5 minutes / 0:30)

Once you've made an initial selection, you can dig deeper using traversal methods.

.children()

var ulChildren = $("ul").children()
console.log(ulChildren)
Vanilla Javascript syntax
document.getElementById("red").children

.parent()

var redParent = $("#red").parent()
console.log(redParent)
Vanilla Javascript syntax
document.getElementById("red").parentNode

.siblings()

var redSiblings = $("#red").siblings()
console.log(redSiblings)

.eq()

var getRed = $("li").eq(0)
console.log(getRed)
Vanilla Javascript syntax
document.getElementById("myID").childNodes[2]

This implementation uses square brackets to access a child node within a collection.

You Do: Selecting DOM elements (30 minutes / 1:00)

20 minutes exercise. 10 minutes review.

Spend 20 minutes completing this exercise.

Break (10 minutes / 1:10)

Get/Set (10 minutes / 1:20)

.html()

.html() can both retrieve ("get") and change ("set") the HTML contents of a DOM node

  • Get: if no argument is passed into the method, it retrieves the HTML content of the first DOM node in the selected collection
  • Set: if an argument is passed into the method, it will replace the HTML content of all selected DOM nodes with that argument
// Get
var getInner = $("#red").html();
console.log(getInner);
// Set
$("ul").html("<li id='blue'>blue</li>");
Vanilla Javascript syntax
document.querySelector("ul").innerHTML = "<li>Blue</li>"

.text()

Similar to .html() except that it will not turn markup into elements and will keep strings intact

  • Get: returns the content of the selected element as a string
  • Set: removes all of the elements children and replaces with whatever newContent is
// Get
var getText = $("ul").text();
console.log(getText);
// Set
$("ul").text("<li>blue</li>");
Vanilla Javascript syntax
document.querySelector("ul").textContent = "<li>Blue</li>"

You Do: Documentation Dive (15 minutes / 1:35)

10 minutes exercise. 5 minutes review.

Find a partner, research, and provide an example of getting and setting using the below methods. Be prepared to share your findings with the class.

jQuery Solution

.css()

// Get
var bodyBackgroundColor = $("body").css("background-color")
console.log(bodyBackgroundColor)
// Set
$("body").css("background-color", "lemonchiffon")

.attr()

.attr() is use to get/set the value of HTML attributes

  • Get: returns the value of an attribute for the first element in the set of matched elements
  • Set: set the value of an element attribute
// Get
var language = $("html").attr("lang")
console.log(language);
// Set
$('img').attr('src','http://www.clipartlord.com/wp-content/uploads/2014/05/unicorn4.png')

.val()

<input type="text" value="name">
// Get
var myName = $("input").val();
console.log(myName)
// Set
$("input").val("Nayana Davis");
Vanilla Javascript Solution
.style()
.setAttribute();
.value

Adding Content (5 minutes / 1:40)

.append()

Adds an element to the end of a parent element, making it the last child.

var blue = $("<li>blue</li>");
$("ul").append(blue);
Vanilla Javascript syntax
.appendChild()

.prepend()

Adds element to the start of a parent element, making it the first child.

$("ul").prepend("<li id='blue'>blue</li>");
Vanilla Javascript syntax
.insertBefore()

You Do: Document Dive (10 minutes / 1:50)

5 minutes exercise. 5 minutes review.

Find a partner, research and provide an example for each of the below methods. Be prepared to share your answer.

Removing/Showing/Hiding Content (5 minutes / 1:55)

.remove()

Removes element from DOM.

$("#red").remove();
Vanilla Javascript syntax
.removeChild()

.empty()

Removes all the child elements of the jQuery object it is called on.

$("ul").empty();

.hide()

Changes elements style to have display: none

$("#red").hide();

.show()

Changes a display: none to display: block or whatever it was initially.

<li id="red" style="display: none;">red</li>
$("#red").show();

You Do: Document Dive (10 minutes / 2:05)

5 minutes exercise. 5 minutes review.

For the remaining three methods, find a partner, research and provide an example for each. Be prepared to share your answer.

You Do: Logo Hijack (15 minutes / 2:20)

10 minutes exercise. 5 minutes review.

  1. Open up the Microsoft website in Chrome or Firefox, and open up the console
  2. Find an image url for the apple logo
  3. Store the url to the apple logo in a variable
  4. Find the Microsoft logo using Javascript and store it in a variable
  5. Modify the source of the logo image so that it's an Apple logo instead
  6. Find the Microsoft search input and store it in a variable
  7. Modify the placeholder of the input so that it says "Search Apple" instead

Bonus

Add a new item to the navigation between the image and the search box. It should contain the text "Apple is the new Microsoft".

.each

With jQuery we might want to do something to each element. Say we have 5 paragraph tags in our html...

<p>Paragraph #1</p>
<p>Paragraph #2</p>
<p>Paragraph #3</p>
<p>Paragraph #4</p>
<p>Paragraph #5</p>

Say we wanted to add a class to each paragraph. We can do something like this...

$("p").addClass("pizza")

This add the class of pizza to each of the paragraph tags. This is happening through something called "implicit iteration" in jQuery. Under the hood Javascript is looping through each paragraph tag and adding a class. What if we wanted to log the HTML to the console? We might try this...

console.log($("p").html())

Hmm, that only gives us the first paragraph's HTML. We need a way to iterate over each of the paragraph tags. Enter .each...

$("p").each(function(){
  console.log($(this).html())
})

jquery-dom's People

Contributors

nayana487 avatar andrewsunglaekim avatar amaseda avatar jshawl avatar timfoley avatar andywhitley avatar nickoki 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.