Turning your static websites into dynamic apps
alecortega
Senior Front End Engineer
HubSpot
slides.com/alecortega/js-libraries
jQuery and Underscore
Prototyping interactions with real world examples
Best practice knowledge
<script src="./path/to/file.js" />
</body>In order to include JavaScript on your page you're going to want to include a script tag right before the closing body tag.
Of the top 10,000 visited websites use jQuery
Document Object Model
<div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
</div>Startup Institute rocks!
View More
Kinda....
<div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
</div>Here we have two elements (<p> and <a>) nested inside of a <div> with the class of container.
<div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
</div>The <p> and <a> tag are both nested at the same level.
<div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
</div><div>
<p>
<a>
<div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
<div class='other-container'>
<p>Awesome reason one</p>
<p>Cool reason two</p>
</div>
</div><div class='container'>
<p>Startup Institute rocks!</p>
<a href='/some/url'>View More</a>
</div>View More
Startup Institute Rocks!
Awesome Reason One
Cool Reason Two
Startup Institute rocks!
View More
<p>
<a>
<div>
<div>
<p>
<p>
windowAn object that has properties that pertain to your browser, and has functions that allow you to change it.
documentAn object that has properties that pertain to your website (the DOM), and has functions that allow you to change it.
Code that someone else has already written that you can pull into your own projects and use the functionality that they already built.
Allows you to easily get grab things from your DOM and do things with it.
<button id='submit-button'>Submit</button><button id='submit-button'>Submit</button>$('#submit-button');$('#submit-button');You pass the same selector you would in your CSS as a string. This is what creates a jQuery representation of your DOM element!
function add(numberOne, numberTwo) {
return numberOne + numberTwo;
}var person = {
firstName: 'Alec',
lastName: 'Ortega',
birthday: 'July 4th'
};var person = {
firstName: 'Alec',
lastName: 'Ortega',
birthday: 'July 4th'
};
console.log(person.firstName);var person = {
firstName: 'Alec',
lastName: 'Ortega',
birthday: 'July 4th'
};
console.log(person.firstName);
// 'Alec'function $(cssSelector) {
return jQueryObject;
}function $(cssSelector) {
return {
hide: function () {
// Hide this element
},
addClass: function() {
// Add class to this element
}
};
}$('#submit-button');$('.container');$('li:first-child');$('h1, div, p');Grabbing an element from the page and directly changing it.
$('#submit-button');$('#submit-button').doSomething();<button id='submit-button'>Submit</button>$('#submit-button');$('#submit-button').text('Submitting');<button id='submit-button'>Submitting</button>$('#submit-button').text('Submitting');
$('#submit-button').addClass('disabled');<button id='submit-button' class='disabled'>Submitting</button>$('#submit-button').text('Submitting');
$('#submit-button').addClass('disabled');
$('#submit-button').doSomething();
$('#submit-button').doSomething();
$('#submit-button').doSomething();
$('#submit-button').doSomething();
$('#submit-button').text('Submitting');
$('#submit-button').addClass('disabled');<button id='submit-button' class='disabled'>Submitting</button>$('#submit-button').text('Submitting');
$('#submit-button').addClass('disabled');$('#submit-button').text('Submitting');$('#submit-button').text('Submitting').addClass('disabled');$('#submit-button').text('Submitting').addClass('disabled');Really just means that you can call one method after another on the same jQuery object.
.append('<h1>Text</h1>');.hide();.addClass('.active');Listening for a user's interaction and then doing something when it happens.
<button id='submit-button'>Submit</button>$('#submit-button');$('#submit-button').on('click', function() {
// Do something
});$('#submit-button').on('click', function() {
// Do something
});$('#submit-button').on('click', function() {
// Do something
});I want the element with the id of submit-button and on 'click' of that element I want to do this.
<button id='submit-button'>Submit</button>$('#submit-button').on('click', function() {
// Do something
});$('#submit-button').on('click', function() {
$('#submit-button').text('Submitting').addClass('disabled');
});$('#submit-button').on('click', function() {
$('#submit-button').text('Submitting').addClass('disabled');
});1. I want the element with the id of submit-button and on 'click' of that element...
2. ...I want the element with the id of submit-button and change the text to "submitting" then add the class of "disabled"...
$('#submit-button').on('click', function() {
$('#submit-button').text('Submitting').addClass('disabled');
});$('.submit-button').on('click', function() {
$('.submit-button').text('Submitting').addClass('disabled');
});<button class='submit-button'>Submit</button>$('.submit-button').on('click', function() {
$('.submit-button').text('Submitting').addClass('disabled');
});<button class='submit-button'>Submit</button>
<button class='submit-button'>Submit</button>$('.submit-button').on('click', function() {
$('.submit-button').text('Submitting').addClass('disabled');
});<button class='submit-button'>Submit</button>$('.submit-button').on('click', function() {
$('.submit-button').text('Submitting').addClass('disabled');
});When you select a DOM object and manipulate it, the change will occur to all elements that match the selector.
$('.submit-button').on('click', function() {
$('.submit-button').text('Submitting').addClass('disabled');
});$('.submit-button').on('click', function() {
$(this).text('Submitting').addClass('disabled');
});"this" is a pretty complex JavaScript concept. But in the context of jQuery all you need to know is that "this" refers the EXACT element that the event is happening to.
$('.submit-button').on('click', function() {
$(this).text('Submitting').addClass('disabled');
});1. I want the element with the id of submit-button and on 'click' of that element...
2. ...take the same element and change the text to "submitting" then add the class of "disabled".
.on('click'.on('input'.on('keypress'Whenever I click on the "Read More" button...
The corresponding panel should slide down to reveal the rest of the article.
<div class='container'>
<h3>Title One</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div><div class='container'>
<h3>Title One</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>
<div class='container'>
<h3>Title Two</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div><div class='container'>
<h3>Title One</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>
<div class='container'>
<h3>Title Two</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>$('.read-more')$('.read-more').on('click', function () {
// Do Something
});$('.read-more').on('click', function () {
$('.container')?
$(this)?
});Allows you to navigate the DOM tree to find a specific element given an original element.
<div class='container'>
<h3>Title One</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>
<div class='container'>
<h3>Title Two</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>$('.read-more').on('click', function () {
$(this).parent();
});$('.read-more').on('click', function () {
$(this).parent().addClass('open');
});<div class='container open'>
<h3>Title One</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>
<div class='container'>
<h3>Title Two</h3>
<p>This is the article...</p>
<a class='read-more'>Read More..</a>
</div>$('.read-more').on('click', function () {
$(this);
});.closest('.class'.find('button'.parent()The jQuery documentation is amazingly helpful and tells you how to do just about anything with jQuery.
$('.element').hide();$('.element').append('<h1>Hi</h1>');$('.element').slideUp();There are hundreds of plugins. All they do is add more methods that you can call on jQuery objects!
Cascading grid layout library
The last carousel you'll ever need
Great API documentation
<script src="/path/to/jquery.js"></script>
<script src="./path/to/yourFile.js"></script>
Include jQuery before any JavaScript that you write. Otherwise it won't know it exists!
Your src for jQuery will probably be a url to a CDN where the file is hosted.
HubSpot
$(el).hide();el.style.display = 'none';$.getJSON('/my/url', function(data) {
});var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();When you click on the corgi, the number should increment.
$('.class').doSomething;function $(cssSelector) {
return {
hide: function () {
// Hide this element
}
};
}We know that when we type .method we're just accessing a function on an actual object returned by the $ function
$('.class').doSomething;$('.class').doSomething;_.doSomething();var _ = {
doSomething: function () {
// Hide this element
}
}Underscore is just an object that has a bunch of named functions that we can call.
Change the DOM or page in anyway.
Gives you a bunch of "utility" functions. Aka. useful functions that help you perform certain tasks, faster, and more easily.
Using only "vanilla" JavaScript, what if I wanted an array of all keys within a certain object. Meaning...
var personEyeColors = {
joe: 'blue',
kiana: 'brown',
rose: 'hazel',
sean: 'brown'
};
['blue', 'brown', 'hazel', 'brown'];
var personEyeColors = {
joe: 'blue',
kiana: 'brown',
rose: 'hazel',
sean: 'brown'
};=> eyeColors
=> ['blue', 'brown', 'hazel', 'brown'];var personEyeColors = {
joe: 'blue',
kiana: 'brown',
rose: 'hazel',
sean: 'brown'
};
var eyeColors = [];
for (var key in personEyeColors) {
var value = personEyeColors[key];
eyeColors.push(value);
}var personEyeColors = {
joe: 'blue',
kiana: 'brown',
rose: 'hazel',
sean: 'brown'
};=> eyeColors
=> ['blue', 'brown', 'hazel', 'brown'];var personEyeColors = {
joe: 'blue',
kiana: 'brown',
rose: 'hazel',
sean: 'brown'
};
var eyeColors = _.values(personEyeColors);_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]_.contains([1, 2, 3], 3);
=> trueLists all the methods you can use
I want to create an "infinite scroll" page (like the Facebook newsfeed), where if I scroll to the bottom of the page, a function is called that adds new content to the DOM.
window.innerHeight + window.scrollY >= document.body.offsetHeight - 50function isAtBottomOfPage () {
//...code that does that logic
}...and all I want you to care about is that it returns TRUE when you've hit the bottom and FALSE when you haven't.
$(window).on('scroll',$(window).on('scroll', function () {
// If I'm at the bottom of the page
// Append the return of fetchData to
// the element that has all the cards
});var lazyLayout = _.debounce(calculateLayout, 300);
$(window).resize(lazyLayout);Makes it so that the passed in function can only fire every "x" number of milliseconds.
$('.card').on('click', function () {
$(this).toggleClass('expanded');
});
$(window).on('scroll', function() {
if(isAtBottomOfPage()) {
$('.container').append(fetchData());
}
});
Advanced Animation Framework
The de-facto library for complex data visualization
Render 3D scenes in the browser
View this slide deck at: