Presentation is loading. Please wait.

Presentation is loading. Please wait.

An introduction to jQuery

Similar presentations


Presentation on theme: "An introduction to jQuery"— Presentation transcript:

1 An introduction to jQuery
Jeremy Shafer Department of MIS Fox School of Business Temple University

2 Objectives Describe jQuery.
Describe how to include the jQuery library in your web pages. Understand the role of jQuery selectors, methods, and events. Repeat and recall the syntax of a simple jQuery selector. Repeat and recall the syntax of the html and append jQuery methods . Apply the pattern of jQuery syntax to val and other new methods.

3 How do I get jQuery? Surprise! You have had jQuery available to you for most of the semester. Try opening hello.html (in intro_jquery.zip) in Chrome. Try opening it with and without line 9.

4 Linking to the jQuery library
Pro tip! Knowing what a CDN URL is, and how to add one to your solution, is a powerful way to extend the options available to you, the programmer.

5 jQuery Described So… Just like Bootstrap is a framework created to simplify CSS, jQuery is a library created to simplify JavaScript. It’s the same sort of idea: make the developer’s life easier, not harder! jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. jQuery is the most popular JavaScript library in use today. jQuery is free, open-source software licensed under the MIT License. Initial release date: August 26, 2006; 13 years ago Created by American software engineer and entrepreneur, John Resig. John Resig is an alumni of the Rochester Institute of Technology. He was going to call it jSelect, but that name was already taken. That would have been a *great* name, because “selecting” is a fundamental concept in jQuery

6 What do you mean by “selecting”?
jQuery relies heavily on the notion of selecting. Recall this example from last week. When we say: $('#mymessage').html('Hello world!’); The browser selects the tag with the id “mymessage” #mymessage is called the selector ! We take action on the tags found. So, the basic structure of a jQuery command is: $(“some selector goes here”).SomeMethod();

7 jQuery selectors DISCUSS: These are the most simple examples of jQuery selectors. What does this syntax/convention remind you of?

8 Once you find the tag(s)
Again, the basic structure of a jQuery command is: $(“some selector goes here”).SomeMethod(); Here’s a new, useful, jQuery method: .addClass("css class name"); DISCUSS: What are two jQuery methods that you have already used this semester? (Use this to add a existing css class to a tag or tags!) This methods and others can work on multiple elements. The jQuery methods work on *all the elements* returned by a selector. (And you don’t even need to write a loop to do it!) Use hello02.html to demonstrate the addClass method, and different selectors

9 More useful jQuery methods
.removeClass("css class name") .toggleClass("css class name") .html(value) .append(value) .val() .val(value) Remove a css class Toggle a css class Putting content in to a tag Retrieve and assign the value of a tag Let’s see jQuery at work! (see hello03.html through hello05.html)

10 Make sure you catch this…
Notice that we are passing “30” into the value of the #gallons tag. Here, #gallons is the id of an <input> tag. We assign the value 30 that by passing is as an argument to the val method. We don’t use the assignment operator ( = ) here!

11 $(“some selector goes here”).next().SomeMethod();
The next() method Here’s our last method for the day … the next() method. What makes this method interesting is that it used in conjunction with other jQuery elements as follows: $(“some selector goes here”).next().SomeMethod(); The next() method allows us to manipulate the tag that is next to the selected tag. This technique of using two or more methods together is called chaining. Let’s see hello06.html

12 Events (again)

13 Events (again) – the jQuery ready event
I will do it this way.

14 Events (again) – the jQuery click event

15 Why this is a big deal… Way back at the beginning of the semester we said that JavaScript could be used to manipulate the HTML and CSS in the browser in response to an event. We’ve arrived! That’s what we’ve been doing today! HTML CSS JavaScript

16 jQuery Summary Methods Selectors .html() . for classes .append()
.val() .addClass() .removeClass() .toggleClass() .next() Selectors . for classes # for id No prefix for tags Events .ready() .click() We will use these as our core jQuery functions. When we need to add a new event or method we’ll draw your attention to it!

17 Next time… Next time we meet, we will use JavaScript and our new jQuery building blocks to create a user interface. We will be working with HTML forms, collecting data, checking the data, and changing the appearance of the page based on what the user does. At last! We have all the pieces we need to do this!


Download ppt "An introduction to jQuery"

Similar presentations


Ads by Google