The mini jQuery-like library. I wanted to see how many features I could fit into around 1kb of code (after minification). Loosely based off of ki.js (https://github.com/dciccale/ki.js), with more features added.
To install, simply include the script anywhere on your page:
<script src="pocket.min.js"></script>
To select an element, pass any valid CSS selector into $():
$('#button')
$('.list')
$('a')
$('#wrapper ul > li:first-child')
You can find a descendant element in the current set of matched elements by using the find() method:
$('.list').find('.item')
... Note that passing any falsy value will simply return an empty array.
You can also pass any DOM object into $():
$(document)
Passing a function to $() ensures that it will not run until the DOM is ready:
$(function(){
console.log('The DOM is ready!');
})
To add an event listener, use the on() method. Pass the event name and callback as arguments:
$('#button').on('click', buttonHandler);
To remove an event listener, use the off() method. Pass the event name and callback as arguments:
$('#button').off('click', buttonHandler);
To remove all event listeners for an event, omit the callback argument:
$('#button').off('click');
To trigger an event, use the trigger() method. Pass the event name as an argument:
$('#button').trigger('click');
To send an AJAX GET request, use the get() utility method. Pass the url and callback as arguments:
$.get('someurl.php', callbackFunction);
To send an AJAX POST request, use the post() utility method. Pass the url, data, and callback as arguments:
$.post('someurl.php', 'hello world', callbackFunction);
To loop through each element in a returned collection of elements, use the each() method. Note that the value of this points to the current element in the loop:
$('a').each(function() {
console.log(this.href);
});
To check if a class exists, pass the class name to the hasClass method:
$('a').hasClass('link');
To toggle a class, use the toggle() method:
$('a').toggle('link');
To toggle the display of an element, call the toggle() method without passing any arguments:
$('a').toggle();
To modify the text or html of an element, use the html() method:
$('a').html('click me');
PocketJS supports the chaining of methods, which means the following is possible:
$('a').html('click me').toggle('active');
Chrome 8+, Firefox 3.6+, Safari 5.1+, Opera 11.5+, IE10+