Please read Chapters 5 - 7
jQuery is a library that helps us do #2, change the UI and respond to user-driven events.
Anytime a user scrolls, clicks, hovers, or types in a form field, we want something to change on the screen.
Sometimes, we only want to change one thing on the screen. We want the browser to only change that one thing in the UI, not reload the entire site.
Let's talk about how the browser actually reads your code....
HTML and JavaScript were not written to interact with each other.
document;
We have explored the D for Document.
We have explored the O for Object.
What about the M for Model?
document
.Please read Chapter 5
jQuery is a collection of JavaScript code. This is called a library.
A library is a collection of reusable methods for a particular purpose. (Like Bootstrap for CSS)
jQuery uses the DOM to find the nodes to apply JS.
JQuery is JavaScript.
jQuery has its own syntax, which we will cover.
To use jQuery (just like Bootstrap in CSS), we add the jQuery library (or CDN) script tag into the HTML file.
Best practice:
Put the script tag right before the closing body tag.
Google has a website for all of the links needed to add JavaScript Libraries.
Go to Hosted Libraries.
Let's create a project that employs our jQuery CDN.
$("HTML-element-selector")
$("HTML-element-selector").method();
Notice for the selector, the parentheses must accept a string as a parameter.
Notice the dot notation for methods to call.
Notice that parentheses are used to call the method.
Here is an HTML element:
<div id="greeting">Hello!</div>
jQuery selecting that HTML element:
$("#greeting")
You can use a selector just like the id selector in CSS.
Here is an HTML element:
<div class="greeting">Hello!</div>
jQuery selecting that HTML element:
$(".greeting")
You can use a selector just like the class selector in CSS.
Write out the jQuery selector for each of the following HTML elements. Click forward to see the answer!
Welcome!
Welcome!
Welcome!
Welcome!
Check this out!
Then do this mix and match!
Once an HTML element is selected as a jQuery selector, we can then use jQuery methods to change it.
For example, when you click on a button, you want the button to do something (e.g., hide/show, alert, tooltip).
Another example: when you submit a form, you want the form to clear its contents.
In addition to changing elements when the document loads or depending on certain media queries, we also need to change the UI based on events.
Events are when the user takes an action on your site. Examples: clicks a button, types in a form, hovers over a link, scrolls down the page, etc.
You can use the .on()
method to create certain events!
$("img").on("click", function() {
//let's do something!
});
Are there different events that you can use instead of just "click"?
In this Codepen, use the .show()
and the .hide()
methods to change the paragraphs!
$("button").on("click", function(){
$("button").css("background-color", "blue")
});
Change my Color -- use this jsfiddle link.
When the user clicks the "Red" Button, the text "Change My Color" should be changed to red.
When the user clicks the "Blue" Button, the text "Change My Color" should be changed to blue.
Start a root folder. Add your libraries and links. Here is the HTML you can add to your HTML file:
<button id="clicker">Click me to show a hidden secret!</button>
<div id="secret" style="display: none">I am a hidden secret.</div>
See next slide for details.
Please read Chapter 6
Instead of writing :
$(".warning").hide();
$(".warning").fadeIn(3000);
You can write:
$(".warning").hide().fadeIn(3000);
As long as you are using the same selector, you can chain multiple methods.
Notice the semi-colon is after the last method in the chain.
Study the code first! The CSS has been written for you.
Code Pen Challenge here.
.on()
with the argument "submit" must be applied to a form
element and tells the browser to submit every input that user has entered into an input field.
$("form").on("submit", function(e){
e.preventDefault(); //this prevents the form from refreshing
var number = $("input").val(); // this allows us to store the value input by the user into a variable.
console.log(number);
});
Notice .on()
accepts two arguments here, a string and a function.
Explain the following code, line by line.
//Version 1
$("#user-form").on("submit", function(e){
e.preventDefault()//This prevents the form from refreshing the page and wiping all of the user's inputs.
var userEmail = $("#user-email").val();
$("div.welcomeBox").append("Welcome "+ username + "
")
});
//Version 2
$("#submit-button").on("click", function(e){
e.preventDefault()//This prevents the form from refreshing the page and wiping all of the user's inputs.
var userEmail = $("#user-email").val();
$("div.welcomeBox").append("Welcome "+ username + "
")
});
Create a form in HTML that gets the user's name, email, and phone number.
Display these values to the user in a paragraph in the UI when the user submits the form.
Watch here
Please read Chapter 7
Thank you for your attention!