Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
Variables
var name = "Ali"; var greeting = "Hey there, Ali"; var greeting = "Hey there, " + name; console.log(greeting);
Data Types
var myString = "This is a string!"; var myInteger = 1; var myBoolean = true; var myArray = ["string", 1, myVar, true]; var myObject = { name: "Pamela", adjective: "Cool", roles: ["hacker", "teacher", "coder"] }
Functions
var name = "Ali"; function greet(){ console.log("Hey there, " + name); } greet();
Functions
var name = "Ali"; function greet() { name = "Joey"; console.log("Hey there, " + name); } greet();
Functions
function add(number1, number2){ console.log("Total: " + (number1 + number2)); } add(2, 3); add(1000, 30000);
Conditionals
var total = 10; if (total >= 100) { console.log("Total is greater than or equal to 100"); } else { console.log("Total is less than 100"); }
Traversing the DOM
document.getElementById('presentation'); document.getElementsByClassName('slide'); document.getElementsByTagName('body'); document.querySelectorAll('a'); document.querySelector('img');
We made it through the JS review!
Any questions?
To hide images:
var elems = document.getElementsByTagName("img"); for (var i = 0; i< elems.length; i++) { elems[i].style.display = "none"; }
To hide images:
$('img').hide();
This HTML...
<img src="cats.jpg">
with this jQuery...
$('img').hide();
results in this HTML...
<img style="display: none;" src="cats.jpg">
$('img').hide();
$() Global jQuery function. Alternately "jQuery()" Used to trigger all jQuery functionality.
'img' Argument being passed into the global jQuery function. In this case, a selector to return all 'img' elements. Also accepts variables and html strings. Returns a jQuery collection/object
.hide() jQuery method to hide the element by adding a style of 'display: none;'
; Syntax to conclude a statement
$('img').hide();
All CSS selectors are valid, plus some extras
a all anchors
.blue elements with the class "blue"
p:eq(2) the third paragraph (zero-based count)
[id^="vidLink_"] elements with an id beginning with "vidLink_"
:contains("free") elements that contain the text "free" (case-sensitive)
There are many jQuery methods- like tools in a toolkit!
Like screwdrivers, hammers, wrenches, there are a few main groups that methods fall into:
So break it down even further, a method can be a getter, a setter, or both!
A Getter.hide() Adds an inline style of "display: none" to the element
.show() Adds an inline style of "display: block" to the element
.remove() Deletes element from the DOM
.html() Retrieves html within the selected element
.text() Retrieves only the text within the selected element
.parent() Retrieves the element's direct parent
.children() Retrieves all direct children of the element
.next() Retrieves the next single element at the same level in the DOM tree
.width() Calculates width of element
.height() Calculates height of element
.fadeOut() Gradually decreases opacity of element
.fadeIn() Gradually increases opacity of element
$('.hero img').remove(); $('#banner > span').hide(); $('#banner').children().hide(); $('img:not(.hero img)').fadeOut();
.addClass('your-classname') — Adds your-classname as a class attribute on to the element
.removeClass('your-classname') —Removes your-classname as a class attribute on to the element
.attr('src') —Retrieves the src attribute for the element
.css('background-color')— Retrieves the background color of the element
.append('<h1>Hey!</h1>') —Inserts an h1 tag after the element
$('.hero img').attr('src'); $('#banner > span').addClass('visible'); $('#banner').children().append('!');
.html() — Retrieves html within element
.html('<h1>Hey!</h1>'); —Inserts/Replaces html within element
.text() — Retrieves text within element
.text('Hey you!'); —Inserts/Replaces text within element
.width() —Retrieves the width of element
.width(300)— Sets the width of the element to be 300px
.attr('src', 'sunset.jpg') — Finds the element's src attribute and changes its value to sunset.jpg
.css('background-color', 'green') —Finds the elements background color and changes it to green by adding an inline style, which overrides any background-color that might already be there
Check the jQuery Documentation for info on how each method works!
<a href="http://www.google.com" style="color: green;">Google</a>
$('a').text(); "Google"
$('a').attr('href'); "http://www.google.com"
$('a').css('color'); "green"
<a href="http://www.google.com" style="color: green;">Google</a>
$('a').text('Yahoo!');
$('a').attr('href', 'http://www.yahoo.com');
$('a').css({'color': 'purple', 'text-decoration': 'underline'});
In demo-files/index.html, use jQuery to
var isMobile = screen.width <= 640; if (isMobile) { $('.desktop-nav').hide(); $('.mobile-nav').show(); }
<img class="sale-img" src="images/sale.png"> var currentTime = new Date().getTime(); // sets currentTime variable to current unix timestamp like 1428534443 if (currentTime > 1428595627) { $('.sale-img').attr('src', '/images/ends-today.png'); } // changes sale image to an 'ends today' version if the time is after 7am on Sunday
$('ul li').each(function() { console.log('This is a list item.'); }); $('ul li').each(function(index) { console.log('This is list item #' + index); });
index: the function supplied to "each" is automatically passed the number of the current loop iteration, which will generally match the index of each member of the collection.
this: "this" is the context of any function. Frequently in jQuery, "this" will refer to the element being pointed to by the iterator.
Other built-in methods are sometimes passed other pertinent data.
Use 'this' as an iterator:
$('ul li').each(function() { $(this).append('!'); });
To be more efficient, cache a variable for $(this):
$('ul li').each(function() { var $this = $(this); $this.append('!'); });
In demo-files/index.html, use .each() to credit each listed quote to Ryan Gosling!
Challenge: Give credit at the beginning of each quote
The bread and butter of jQuery.
$('button').on('click', function() { console.log('the button was clicked!'); });
You can also...
$('a').on('click', function (event) { event.preventDefault(); console.log('Not going there!'); });
Event handlers are typically passed the event that has triggered them, and that data can then be used by the handler. See info on the event object: http://api.jquery.com/category/events/event-object/
You can also use the "on" method to bind to multiple events at once:
$('.sidebar').on({ click: function() { $(this).toggleClass('active'); }, focusin: function() { $(this).addClass('inside'); }, focusout: function() { $(this).removeClass('inside'); } });
Most everything you want to do with jQuery requires the DOM to be fully loaded.
The browser renders the markup from top to bottom, then triggers a DOMReady event.
$(document).ready(function() { // Handler for .ready() called. });
$(function() { // Handler for .ready() called. });
Add a slideshow using Slick.js!
http://bit.ly/gdi-intro-jquery
alexandra.hoefinger@gmail.com