Skip to content

Instantly share code, notes, and snippets.

View icodesido's full-sized avatar

Ivan icodesido

View GitHub Profile
@icodesido
icodesido / Cl cl closures
Created October 20, 2014 15:38
Getting closure
var nodes = document.getElementsByTagName('*');
for (var i = nodes.length - 1; i >= 0; i--) {
nodes[i].addEventListener('click', function() {
console.log('You clicked element #' + i);
});
}
var nodes = document.getElementsByTagName('*');
for (var i = nodes.length - 1; i >= 0; i--) {
nodes[i].addEventListener('click', (function(i) {
@icodesido
icodesido / jQuery plugin boilerplate
Created October 17, 2014 15:51
jQuery plugin template
(function($){
$.fn.yourPluginName = function(){
// Your code goes here
return this;
};
})(jQuery);
@icodesido
icodesido / Better than .push
Created October 17, 2014 10:46
Better performing alternative to push
var x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
y = [];
for (var i = 0; i < x.length; i++) {
y[y.length] = x[i];
}
@icodesido
icodesido / Bitwise Xor
Created October 17, 2014 10:15
Toggle with Xor
var x = 0;
// this will toggle between 0 and 1 everytime is reset
x ^= 1;
@icodesido
icodesido / Outline every element
Created October 17, 2014 09:33
Put a randomly coloured outline around every html element.
[].forEach.call($$("*"),function(a){
a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
})
@icodesido
icodesido / Revert name surname
Created October 17, 2014 09:31
Use a regex with placeholders to reverse name and surname
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
@icodesido
icodesido / Wishlist calculator
Last active August 29, 2015 14:04
Amazon wishlist price calculator
prices = jQuery('#item-page-wrapper .price-section .a-color-price').text()
prices = prices.replace(/\s/g,'').split('£');
total = 0;
var v;
for(var i = 0; i < prices.length; i++) {
v = parseFloat(prices[i]);
if (!isNaN(v)) total += v;
}
@icodesido
icodesido / Raw vs jQuery AJAX
Created May 21, 2014 13:51
Raw vs jQuery AJAX
$.getJSON('/my/url', function(data) {
});
vs
request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
@icodesido
icodesido / Random
Last active August 29, 2015 14:01
Generate random 5 char string
function makeId()
{
var txt = "",
rand = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 5; i >= 0; i--) {
txt += rand.charAt(Math.floor(Math.random() * rand.length));
}
return txt;
@icodesido
icodesido / Thunder never strikes...
Created December 10, 2013 12:38
Press the same keystroke twice
var twice_37 = 0;
$(document).on('keydown', function( e ){
var key = e.which;
if(key==37){
twice_37 += 1; // almost :)
if(twice_37==2){
alert('Do something! you pressed twice left!');