That detail is only sort of important That detail is only sort of important. What is important is that you have all of these HTML elements floating.

Slides:



Advertisements
Similar presentations
HTML Basics Customizing your site using the basics of HTML.
Advertisements

The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
Cascading Style Sheets CSS. What is it? Another file format ! its not like html Describes the looks of “selectors” in a.css file (example.css) in the.
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
1 Pengantar Teknologi Internet W03: CSS Cascading Style Sheets.
Recognizing the Benefits of Using CSS 1. The Evolution of CSS CSS was developed to standardize display information CSS was slow to be supported by browsers.
CSW131 Steven Battilana 1 CSW 131 – Chapter 5 (More) Advanced CSS Prepared by Prof. B. for use with Teach Yourself Visually Web Design by Ron Huddleston,
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
Advanced CSS - Page Layout. Advanced CSS  Compound Selectors:  Is a Dreamweaver term, not a CSS term.  Describes more advanced types of selectors such.
INFSCI  Start with a template base structure  Think about how to structure your document using headers, paragraphs, divs, unordered lists, imgs.
INFSCI  Last time we built a doggie web page in class following the instructions in the slide deck: Build Web Page with HTML – see week 3 The topics.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
IDs versus Classes Naming Your Tags for Maximum Functionality.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
CITS1231 Web Technologies JavaScript and Document Object Model.
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
WEB DESIGN AND PROGRAMMING Advanced CSS Techniques.
CSS Cascading Style Sheets A very brief introduction CSS, Cascading Style Sheets1.
- WE’LL LOOK AT THE POSITION PROPERTY AND HOW CSS RESOLVES CONFLICTS BETWEEN STYLING INSTRUCTIONS The position property; CSS specificity hierarchy.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
CSS Layout Cascading Style Sheets. Lesson Overview  In this lesson, we’ll cover:  Brief CSS review  Creating sections with the tag  Creating inline.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Tarik Booker CS 120 California State University, Los Angeles.
Mimi Opkins.  One of the major benefits of using CSS is that you’re not forced to lay your sites out in tables.  The layout possibilities of CSS give.
Introduction to CSS: Selectors
Cascading Style Sheets for layout
Working with Cascading Style Sheets
Online PD Basic HTML The Magic Of Web Pages
Programming Web Pages with JavaScript
CNIT 131 HTML5 - Tables.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unit M Programming Web Pages with
4.01 Cascading Style Sheets
Webpage layout using CSS
CGS 3066: Web Programming and Design Spring 2017
Scrolling Down vs. Multiple Pages
Bare bones notes.
Unit M Programming Web Pages with
IS 360 Declaring CSS Styles
CGS 3066: Web Programming and Design Spring 2016
Cascading Style Sheets for layout
AN INTRODUCTORY LESSON TO MAKING A SIMPLE WEB PAGE By: RC Emily Solis
JavaScript Functions.
Programming the Web using XHTML and JavaScript
Arrays and files BIS1523 – Lecture 15.
Writing JavaScript Code
Introduction to web design discussing which languages is used for website designing
CSS Applications to XML 19-Sep-18.
Styles and the Box Model
Second CSS Lecture Applications to XML
Web Programming Language
IS 360 Understanding CSS Selectors
Tutorial 8 Designing a Web Site with Frames
Lesson Objectives Aims You should know about: – Web Technologies
Introduction to jQuery
Bare bones notes.
More CSS 22-Feb-19.
Tutorial 4 Creating Special Effects with CSS
Part 1: Cascading Style Sheets
Web Programming Language
Web Development 101 Workshop
The Internet 10/20/11 CSS Layout
4.01 Cascading Style Sheets
5.00 Apply procedures to organize content by using Dreamweaver. (22%)
CSS Applications to XML 20-May-19.
The div Element and CSS for layout
CSS Applications to XML 3-Jul-19.
© 2017, Mike Murach & Associates, Inc.
Presentation transcript:

That detail is only sort of important That detail is only sort of important. What is important is that you have all of these HTML elements floating around that you want to access and read data from or modify. There are many ways to find these HTML elments. After all, these elements are arranged in a tree-like structure, and if there is one thing computer scientists like to do is figure out crazy ways to run up and down a tree to find something. Meet the querySelector Family To help explain the awesomeness that querySelector and querySelectorAll bring to the table, take a look at the following HTML:

<div class="pictureContainer"> <div id="main"> <div class="pictureContainer"> <img class="theimage" src="smiley.png" height="300" width="150"/> </div> <img class="theimage" src="tongue.png" height="300" width="150"/> <img class="theimage" src="meh.png" height="300" width="150"/> <img class="theimage" src="sad.png" height="300" width="150"/> In this example, you have one div with an id of main, and then you have four div and img elements each with a class value of pictureContainer and theimage respectively. In the next few sections, we'll set the querySelector and querySelectorAll functions loose on this HTML and see what happens.

QuerySelector The querySelector function basically works as follows: var element = document.querySelector("< CSS selector >"); The querySelector function takes an argument, and this argument is a CSS selector for the element you are wanting to find. What gets returned by querySelector is the first element it finds - even if other elements exist that could get targeted by the selector. This function is pretty stubborn like that. Taking the HTML from our earlier example, if we wanted to access the div whose id is main, you would write the following: var element = document.querySelector("#main");

Because main is the id, the selector syntax for targeting it would be #main. Similarly, let me specify the selector for the pictureContainer class: var element = document.querySelector(".pictureContainer"); What gets returned is the first div whose class value is pictureContainer. The other div elements with the class value of pictureContainer will simply be ignored. The selector syntax is not modified or made special because you are in JavaScript. The exact syntax you would use for selectors in your stylesheet or style region can be used!

QuerySelectorAll The querySelectorAll function returns all elements it finds that match whatever selector you provide: var elements = document.querySelectorAll("< CSS selector >"); With the exception of the number of elements returned, everything I described about querySelector above applies to querySelectorAll as well. That important detail changes how you end up actually using the querySelectorAll function. What gets returned is not a single element. Instead, what gets returned is an array of elements! Continuing to use the HTML from above, here is what our JavaScript would look like if we wanted to use querySelectorAll to help us display the src attribute of all the img elements that contain the class value theimage:

var images = document.querySelectorAll(".theimage"); for (var i = 0; i < images.length; i++) { var image = images[i]; alert(image.getAttribute(src)); } See, pretty straightforward. The only thing you need to do is remember how to work with Arrays. If you aren't familiar with getAttribute and how to read values from elements, that's something we'll look at really soon.

It Really is the CSS Selector Syntax The thing that surprised me when I first used querySelector and querySelectorAll is that it actually takes the full range of CSS selector syntax variations as its argument. You don't have to keep it simple like I've shown you so far. If you wanted to target all of the img elements without having to specify the class value, here is what our querySelectorAll call could look like: var images = document.querySelectorAll("img"); If you wanted to target only the image whose src attribute is set to meh.png, you can do the following: var images = document.querySelectorAll("img[src='meh.png']");

Note that I just specified an Attribute Selector as my argument to querySelectorAll. Pretty much any complex expression you can specify for a selector in your CSS document is fair game for specifying as an argument to either querySelector or querySelectorAll. There are some caveats that you should be aware of: Not all pseudo-class selectors are allowed. A selector made up of :visited or :link is ignored and no elements are found. How crazy you can go with the selectors you provide depends on the browser's CSS support. Internet Explorer 8 supports querySelector and querySelectorAll. It doesn't support CSS3. Given that situation, using anything more recent than the selectors defined in CSS 2 will not work when used with querySelector and querySelectorAll on IE8. The selector you specify only applies to the descendants of the starting element you are beginning your search from. The starting element itself is not included.

What about getElementById, getElementsByTagName, getElementsByClassName? The querySelector and querySelectorAll functions are the new kids on the block. The past was dominated by the getElementById, getElementsByTagName, and getElementsByClassName functions that pretty much did exactly as their names describe. You may still see these functions used in older books, tutorials, and snippets online. In general, you should just use querySelector and querySelectorAll. These two functions are easier to use and far more powerful than what you could do with the getElement* functions. Like a wise person once said, life is too short to spend time learning about old JavaScript functions.