"Where things start to get, well, difficult..."
Proposals needed.
Not just attending, but presenting
What did you come up with?
Rank Top 5
Write Top 3
If tied, write 4th then 5th
var matches = function(el, selector) {
var _matches = (el.matches || el.matchesSelector ||
el.msMatchesSelector || el.mozMatchesSelector ||
el.webkitMatchesSelector || el.oMatchesSelector);
if (_matches) { return _matches.call(el, selector);
} else {
var nodes = el.parentNode.querySelectorAll(selector);
for (var i = nodes.length; i--;) {
if (nodes[i] === el)
return true;
} return false; }
};
matches(el, '.my-class');
$(el).is('.my-class');
It makes JavaScript easier.
$
By CSS selector
$('.my-class');
$('.my-class').html();
$('.my-class').fadeIn();
var myHtml = $('.my-class').html();
$('.my-class').html('<p>Hello World!</p>');
$('#foo').slideDown();
$('.foo').fadeOut('fast');
$('.foo').text();
Named
var eric = function() {};
eric();
Anonymous
function() {};
Run within another function to execute immediately.
Anonymous or named
$('.foo').click(function(){});
$('.foo').click(eric);
$('.my-class').html(function(){
var myHtml = '<p>Hello World!</p>';
return myHtml;
);
"callback"
$('.my-class').fadeIn("slow", function(){/*complete*/});
$('.my-class').click(function(){});
Event Delegation
$('.foo').on('click', '.my-class', function(){});
$('.foo').on('click', '.my-class', function(e){
e.preventDefault();
});
Prevent Default Event
$('.foo').text();
$('.foo').fadeIn();
$('.foo').slideToggle('fast');
$('.foo').text();
$('.foo').html();
$('.foo').slideToggle('slow');
$('.foo').fadeIn();
$('.foo').click(function(e){e.preventDefault();});
$('.my-class')
.slideToggle('fast')
.text('moar!')
.html(function(index, oldHtml){
return oldHtml + ' Internetz!!';
})
.slideToggle('slow')
.fadeIn(3000)
.click(function(e){
e.preventDefault();
console.log('done!')});
Lots and lots and lots and lots!
$('.foo').addClass('moar');
$('.foo').removeClass('moar');
$('.foo').toggleClass('moar');
$('.foo').text('Hello, World!');
$('.bar').text($('.foo').text());
var fooText = $('.foo').text();
$('.bar').text(fooText);
Works exactly the same for HTML (except for HTML tags).
var fooVal = $('.foo').val();
val() attr()
To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. For everythign else, use attr()
var fooVal = $('.foo').val();
$('.bar').append(fooVal);
before()
wrap(begin)
<span class="bar">prepend() <-- Content --> append()</span>
wrap(end)
after()
appendTo(), prependTo(), insertAfter(), insertBefore()
$( "#greatphoto" ).attr( "title", "Photo by Kelly Clark" );
$( "#greatphoto" ).attr({
alt: "Beijing Brush Seller",
title: "photo by Kelly Clark"
});
You can do this with the .css property too... but don't use add/remove/toggle class.
$('.foo').fadeOut(2000);
fadeIn(), slideUp(), slideDown(), slideToggle()
$('.foo').hide();
$('.foo').addClass('hidden');
Cut as much DOM manipulation as you can.
$('.foo').find('.bar');
More performant than $('.foo .bar');
$('.foo').load('http://google.com');
load() is a super quick AJAX request. Deeper AJAX will come later on
No class next week.
Midterm due 3/31, not 4/7
Foundation 5 Boilerplate (DON'T FORK, download)