Slides available at: bit.ly/CnCjQuery
html / \ head body / \ / \ meta link nav div
Selector - Picks out the element to manipulate
element: div, p, h1, etc #id-selector: #my-id, #btn-submit, etc .class-selector: .my-class, .container, etc
var myIngredient = "Vodka";
Function - Takes input, processes the input and produces output
function cocktailShaker (vodka, cappelletti, lime, sugar, pellegrino) { var cocktail = vodka + cappelletti + lime + sugar + pellegrino; return cocktail; }
Like a <link> tag in the html <head> section connects your CSS, a <script> tag connects your JavaScript
<script src="assets/scripts/jQuery.min.js"></script> <script src="assets/scripts/script.js"></script>
If your script file depends on another script file (as our script file depends jQuery), the dependancy has to be loaded prior to the script that depends on it so in our case the jQuery script tag has to come before our script tag.
$ is an alias for the jQuery function
$('p') = jQuery('p')
Allows for shorter, faster coding
We want our code to be able to run as soon as the page is ready - as soon as the DOM heierarchy has been constructed.
$(document).ready();
$(document).ready(function () { //your jQuery code goes in here });
The function above is referred to as a "callback." This is executable code that is passed as a parameter to another function or piece fo code
we pass the function into the ready function to run whatever code we've placed inside. Functions are objects so we're passing in a function object containing the code we want to have running. This is referred to as a "callback" functions without a name are referred to as anonymous functionsIn order to capture interactions and react to them we need to be able to pick out HTML elements
$() is used to select elements
The this keyword always refers to the element that was clicked and can come in handy for reacting to events.
This creates a jQuery object containing the selected elements that you can call methods (such as .fadeOut() or .html()) onGiven the HTML:
<div class="main"> <h2>My Section Title</h2> <p id="my-id">Paragraph content. Not feeling creative.</p> <div>
Select the h2 with an element selector:
$('h2')
select the div with a class selector:
$('.main')
select the paragraph with an id selector:
$('#my-id')
In the case where an element is nested inside of another, the inner element is the child element and the outer element is the parent element.
<nav> <ul> <li>About</li> <li>Contact</li> </ul> </nav>
ul is a child of the nav element and the nav element is a parent of the ul element.
Parent selection example using jQuery
Given the HTML:
<nav> <ul> <li>About</li> <li>Contact</li> </ul> </nav>
select the nav element using the parent method:
$('ul').parent(); //nav element is selected
Capture the action on an element and tell the browser what to do when it occurs.
Given the HTML:
<div class="display"></div> <form> First name:<br> <input type="text" name="firstname"><br> Last name:<br> <input type="text" name="lastname"> </form> <button>Submit</button>
Gather click event on the Submit button:
$('button').click(function () { //code for what you want to do when //the button is clicked goes here });
These are things that happen when an event is handled!
When you add < & > around the element in the jquery function, you're creating an element instead of selecting it.
$('div')
$('<div>')
The above creates an element but how do we add that element to the page?
Updates and replaces content nested inside the selected element
<div><h5>This will get replaced!</h5></div>
$('div').html('<h2>My Heading</h2>');
<div><h2>My Heading</h2></div>
Adds content inside the selected element
<h1>Page Title</h1> <div id="main-header"></div>
$('#main-header').append('<h2>Sub Heading</h2>');
<h1>Page Title</h1> <div id="main-header"> <h2>Sub Heading</h2> </div>
If we ran the same append a second time we'd get:
<h1>Page Title</h1> <div id="main-header"> <h2>Sub Heading</h2> <h2>Sub Heading</h2> </div>
Sometimes you want to combine the value from a variable with other text. In order to do that we utilize what is called concatenation. In the same way that we use + to add numbers we can use it in jQuery to add strings together.
var myVar = 'Chocolate'; var newString = myVar + ' is delicious!'; //newString = "Chocolate is delicious!"
Keyboard shortcut: cmd+option+i on Macs, cmd+shift+i or F12 on Windows
Work through part 1 of tonight's worksheet
Work through part 2 of Tonight's Worksheet
#LadyDevs
#KCWiT
#CodingAndCocktailsKC