$options = {
productName: '#product-name',
productId: '#product-id'
};
$($options.productName).text('Shoes');
$($options.productId).val(12);
if (something) {
$('#productBox').text('yes');
} else {
$('#productBox').text('no');
}
options = {
$productName: $('#product-name'),
$productId: $('#product-id')
};
options.$productName.text('Shoes');
options.$productId.val(12);
$productBox = $('#productBox');
if (something) {
$productBox.text('yes');
} else {
$productBox.text('no');
}
$productBox.text('yes');
$productBox.val(12);
$productBox.addClass('special');
$productBox.removeClass('default');
$productBox
.text('yes')
.val(12)
.addClass('special')
.removeClass('default');
$('#house .garden');
$('#car wheel[number=4]');
$(this).parents('.tree').children('.bush');
$('#house').find('.garden');
$('#car').find('wheel[number=4]');
$(this).parents('.tree').find('.bush');
var brands = ['nike', 'adidas', 'puma', 'asics', 'clae'],
list = $('.brands');
$.each(brands, function(index, brand) {
list.append('<li data-brand="brand-+ index + ">' + brand + '</li>');
});
http://lab.abhinayrathore.com/jquery-standards (DOM Manipulation)
var brands = ['nike', 'adidas', 'puma', 'asics', 'clae'],
list = $('.brands'),
tmpList = '';
$.each(brands, function(index, brand) {
tmplList += '<li data-brand="brand-+ index + ">' + brand + '</li>';
});
list.html(tmpList);
//array.join('') even faster
$('ul.list li a').on('click', function(){
alert('YO!');
})
//Q: what happens if you add another li with a link?
var $list = $('.list');
$list.on('click', 'a', function() {
alert('YO!');
});
var thisThing = "<div class=\'are you kidding me\'>" + '×' + "</div>");
var thisThing = '<div class="are you kidding me">' + '×' + '</div>');
//
var $list = $('.list');
$list.on('click', 'a', function() {
alert('YO!');
});
//
setTimeout(function(){
....
}, 200);
//
var $list = $('.list');
$list.on('click.listAlert', 'a', listClickHandler);
listClickHandler = function() {
alert('YO!');
}
//not needed? turn it off!
$list.off('click.listAlert');
//
setTimeout(thisOtherFunction, 200);
thisOtherFunction = function() {...};
if (value === 'horse') {
animal = true;
} else {
animal = false;
}
if (value === 'horse') {
animal = true;
return;
}
animal = false;
$('thisBox').hide();
//$('thisBox').show();
[hidden] {
display: none
}
$('thisBox').attr('hidden', true);
//$('thisBox').attr('hidden', false);
$(document).ready(function () {
var numArticles = 8;
function doCheckout() {
alert('doCheckout');
}
$('#js-btn-checkout').click(function() {
doCheckout(numArticles, $('#js-list-articles'));
});
});
var s,
ShoppingCart = {
settings: {
numArticles: 8,
$listArticles: $('#js-list-articles'),
$btnCheckout: $('#js-btn-checkout')
},
init: function() {
s = this.settings;
ShoppingCart.bindUIActions();
},
bindUIActions: function() {
s.$btnCheckout.on('click', ShoppingCart.checkoutHandler);
},
doCheckout: function(maxArticles) {
// proceed to checkout
alert('doCheckout');
},
checkoutHandler: function() {
ShoppingCart.doCheckout(s.numArticles, s.$listArticles);
}
};
(function() {
ShoppingCart.init();
SomeOtherModule.init();
})();