• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/58

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

58 Cards in this Set

  • Front
  • Back
Document Object Model
(DOM)
A tree-like structure created by browsers that JavaScript uses to manipulate HTML elements and other nodes (like text).
.text()
A jQuery method that gets the text value of matched elements.

$('h1').text();
$(document).ready(funtion() {
//Run code here after DOM has finished loading
});
The $(document).ready() method runs jQuery code after the DOM is ready.
$(window).load(function() {
//Run code here after all elements on the page have finished loading including images.
});
The $(window).ready() method runs jQuery code after all elements on the page have finished loading (like images).
$(function() {
//Shorthand to run code here after DOM is ready
});
Shorthand for $(document).ready() & code inside will run after the DOM is ready.
$('#destinations li');
The <li> element is the descendant selector.
$('#destinations > li');
Direct child selector of a parent element.
:first
Select the first child of a matched element.

$('#destinations li:first');
:last
Select the last child of a matched element.

$('#destinations li:last');
$('ul li:odd');

$('ul li:even');
Odd & event pseudo selectors. Index starts at 0 for elements so :even gets 1st <li> below.
<ul>
<li>A</li> [Index is 0 so :even gets element]
<li>B</li> [Index is 1 so :odd gets element]
<li>C</li> [Index is 2 so :even gets element]
</ul>
.find()
A method that gets the descents of the parent element & uses traversing. Also called a traversal.

*Faster than using descendant selectors!

$('#tours').find('li');
.first()
A traversal to find the 1st matched element.

$('li').first();
.last()
A traversal to find the last matched element.

$('li').last();
.next()
.next() finds the next sibling matched element.

The code example uses walking the DOM and method chaining to find the 2nd element.

$('li').first().next();
.prev()
.prev() finds the previous sibling matched element.

The code example uses walking the DOM and method chaining to find the eventually find the 1st element.

$('li').first().next().prev();
.parent()
Gets the matched parent element, and 1 node only, by walking up the DOM.

$('li').first().parent();
.children()
Gets the direct children of the parent node.

$('#tours').children('li');
var price = $('<p>From $399.99</p>');
Code example creates a node but DOESN'T add it to the DOM.
.append()
Adds a node of the last direct child of the matched parent element.

var price = $('<p>From $399.99</p>');

$('.vacation').append(price);
.remove()
Removes the matched element from the DOM.

$('button').remove();
.appendTo()
Insert element as last node in matched parent node.

var price = $('<p>From $399.99</p>');
price.appendTo($('.vacation'));
.prependTo()
Insert element as 1st node in matched parent node.

var price = $('<p>From $399.99</p>');
price.prependTo($('.vacation'));
.insertAfter()
Insert element after matched element as a sibling.

$('<p>Test</p>').insertAfter('.inner');
.insertBefore()
Insert element before matched element as a sibling.

$('<p>Test</p>').insertBefore('.inner');
.insertAfter()
Insert element after matched element as a sibling.

$('<p>Test</p>').insertAfter('.inner');
$(document).ready();
Ready method that run JavaScript after the DOM is ready.
function() {

}
In jQuery this appears inside of the $(document).ready(); method and is called an event handler function.

The $(document).ready(); method takes 1 argument which is the event handler function.
.on(<event>,<event handler>);
A method that attaches event handlers to the currently selected set of elements.

Must be inside the event handler to run when the DOM is ready.

$('button').on('click', function() {
//Code here runs on the specific <button> element that was clicked
});
.closet()
Find ancestor of matched elements.

Finds 0 or 1 nodes of matched elements & .parents() finds all nodes of matched elements.
this
"this" refers to the specific element that an event is happening to. In the code below "this" refers to the <button> element that is clicked.

$('button').on('click', function() {
var price = $('<p>From $399.99</p>');
$(this).closest('.vacation').append(price);
$(this).remove();
});
HTML5 --->>> data* Attributes
The data* attributes are used to store custom data private to a page or application and can be added to all HTML elements.

<ul>
<li data-animal-type="bird">Owl</li>
<li data-animal-type="fish">Salmon</li>
</ul>
.data(<name>)

.data(<name>, <value>)
Store arbitrary data associated with the <data*> attributes of matched elements. In the example below the .data() method stores a "399.99" value.

<ul>
<li class="vacation" data-price="399.99">Hawaiian Vacation</li>
</ul>

$('.vacation').first().data('price');
$('.vacation').on('click', 'button', function() {
//Run event on element that event happened to
});
Event delegation is used so I only target <button> elements inside the <.vacation> element.
.filter() vs .find()
.filter() --->>> Search through all the elements.

.find() --->>> Search through all the child elements only.
.addClass
Add a class to a set of matched elements. The code below adds a <.highlighted> class to the matched elements.

$('.vacation').filter('.onsale').addClass('highlighted');
.removeClass
Remove a class to a set of matched elements. The code below removes a <.highlighted> class to the matched elements.

$('.vacation').filter('.onsale').removeClass('highlighted');
.parent() vs .parents()
HTML used for examples below:
<div class="one"><div class="two">
<p><span>Text</span></p>
</div></div>

parent() --->>> Code below selects <p> which is a single parent node:
$('span').parent();

parents() --->>> Code below selects all parent nodes including <p>, <div.one>, <div.two>, <body>, <html>:
$('span').parents();
var vacation = $(this).closest('.vacation');
Example of assigning a method chain to a variable.
.fadeIn()
.fadeOut()
.fadeToggle()
.fadeToggle() --->>> A method that toggles between .fadeOut() & fadeIn() when an event happens, like a click.

$(this).find('.comments').fadeToggle();
.slideDown()
.slideUp()
.slideToggle()
.slideToggle() --->>>A method that toggles between .slideUp() & slideDown() when an event happens, like a click.

$(this).find('.comments').slideToggle();
alert()
.length
The code below uses the .length object property and alert() method which are reserved words and part of plain JavaScript.

The .length property measures number of a specified object, like the <li> nodes in the example below.

alert($('li').length);
.mouseenter()
This method triggers the mouseenter event or attaches a function to run.

Your mouse must move over an element to fire this method.

$('.confirmation').on('mouseenter', 'button', function() {
//Fire event here
});
function showTicket() {
$(this).closest('.confirmation')
.find('.ticket').slideDown();
}

$(function() {
$('.confirmation').on('click', 'button', showTicket);
});
This code shows an example of refactoring an event handler.

The 3rd parameter in the "on" method fires the "showTicket" function after the "on" method is fired.

The "on" method is also inside the shorthand $(document).ready(); method to ensure it fires after the DOM is ready.
.mouseleave()
This method triggers the mouseleave event, or a attaches a function to run, when a mouseleave event occurs.

Your mouse must leave an element to fire this method.

$('.confirmation').on('mouseleave', 'button', function() {
//Fire event here
});
.keyup()
Bind an event handler to the "keyup" JavaScript event or trigger that event on the element.

The "keyup" event occurs after pressing a key on your computer and then letting your finger up.

Event binding refers to telling the browser that a particular function should be called whenever some event occurs.

$('.vacation').on('keyup', '.quantity', function() {
//Run code here after after "keyup" event
});
How to Convert a String to a Number
In jQuery adding "+" at the beginning of a variable declaration will convert a string that contains numbers into a number type.

var price = +$(this).closest('.vacation').data('price');
.val()
Gets the value inputed to matched elements, like a <input> field.

var quantity = $('input').val();
Converting Numbers to Strings using .text()
The .text() method can be used to get the text value of matched elements AND convert numbers into strings.

The .text() method accepts a number or a string.

In the code below if the "price" and "quantity" variables below are assigned to numbers the final result of their multiplication is converted into a string.

$('#total').text(price * quantity);
.focus()
Bind an event handler to the "focus" JavaScript event, or trigger that event on an element.

The "focus" event fires when an element gains focus, like clicking your mouse inside of an <input> element.

$("#nights").on("focus", function() {
$(this).text("7");
});
event.preventDefault();
If this method is called, the default action of the event will not be triggered. The method does not accept any arguments.

For example, clicked anchors will not take the browser to a new URL.

The code below prevents the web page from jumping to the top, the default behavior, after an anchor is clicked and a hash is added to the URL.

$('.vacation').on('click', '.expand', function(event) {
event.preventDefault();
$(this).closest('.vacations')
.find('.comments')
.fadeToggle();
});
event.stopPropagation();
The event.stopPropagation() method allows the event's default functionality to happen but prevents the event from propagating, or bubbling to parent elements.

The default browser behavior will still occur. This method accepts no parameters.

In the code below the event event will happen on the <p> element yet no events will happen on parents of the <p> element because of the event.stopPropagation() method.

$( "p" ).click(function( event ) {
event.stopPropagation();
// Do something
});
.css(<attr>)
.css(<attr>, <value>)
.css(<object>)
A method to get CSS attributes, get and set CSS attributes or get and set CSS attributes passing an object as a parameter.

The code below uses an object to get and set CSS attributes. Remember that JavaScript object literals have braces around object properties, have colons between property names and values and that the properties are separated by commas except the last one.

$(this).css({'background-color': '#f00',
'border-color': '1px solid #000'});
.show()
.hide()
.toggle()
.show() applies a "display: block" CSS property to matched elements. Parameters are accepted.

$(this).find('.price').show();

.show() applies a "display: none" CSS property to matched elements. Parameters are accepted.

$(this).find('.price').hide();

.toggle() hides and shows an element when an event is fired.

$(this).find('.price').toggle();
.addClass()
.removeClass()
.toggleClass()
.addClass() adds one or more classes to matched elements.

$(this).addClass('highlighted');

.removeClass() removes one or more classes to matched elements.

$(this).removeClass('highlighted');

.toggleClass() adds or removes one or more classes to matched elements when an event is fired.

$(this).toggleClass('highlighted');
.animate(<object>)
Takes a CSS object as a parameter to cause an animation, similar to .css(), when an event fires.

$(this).animate({'top': '-10px'});
.hasClass()
Determine whether any of the matched elements are assigned the given class.

For the code below if the ".highlighted" class exists the ".animate({'top': '-10px'})" event occurs and if the ".highlighted" class does NOT exist the ".animate({'top': '0px'})" event occurs.

$(this).toggleClass('highlighted');

if( $(this).hasClass('highlighted') ) {
$(this).animate({'top': '-10px'});
} else {
$(this).animate({'top': '0px'});
}
.animate(<object>, <speed>)
The .animate method can accept a second parameter that shows the speed of the animations in milliseconds or words like "fast" which represent 200 milliseconds.

The examples below show the words which match up to the milliseconds. When a second parameter for speed is NOT included then a default of 400 milliseconds is added.

$(this).animate({'top': '-10px'});
$(this).animate({'top': '-10px'}, 400);

$(this).animate({'top': '-10px'}, 'fast');
$(this).animate({'top': '-10px'}, 200);

$(this).animate({'top': '-10px'}, 'slow');
$(this).animate({'top': '-10px'}, 600);
.vacation {
-moz-transition: top 0.2s;
-o-transition: top 0.2s;
-webkit-transition: top 0.2s;
transition: top 0.2s;
}

.highlighted {
top: -10px;
}
The transition CSS3 property can add an animation when an event occurs, like hovering over an element.

The code