Skip to content

Instantly share code, notes, and snippets.

View icodesido's full-sized avatar

Ivan icodesido

View GitHub Profile
@icodesido
icodesido / Replace text
Created June 6, 2012 11:21
Change comment count text on the fly
if(jQuery('.discussion .toolbar:contains("Open for comments")').length){
commentCount = jQuery('.content-comment-count');
commentCount.text(commentCount.text().replace('Comments','Jump to comments'));
}
Variants:
1 Jump to comments
2 See all comments
3 Read the comments
4 Readers' comments
@icodesido
icodesido / Most popular in section
Created October 23, 2012 11:02
Most popular in Technology with the styles for the whole promo col
@icodesido
icodesido / Bind and call compared
Created November 28, 2013 17:41
native js vs underscore
function Developer(skill) {
this.skill = skill;
this.says = function(){
alert(this.skill + ' rocks!');
}
}
var john = new Developer('Backbone');
var func = john.says;
func.apply(john);
// backbone.js continues to impress, I needed to get data from a jsonp api
// I really wanted to do this the "right" backbone.js way and create a model and call fetch.
// But the default backbone synch is not jsonp
// Turns out you can override a synch on a per model basis (thanks stackoverflow)
// whats nice is backbone.js continue to work as expected with the override
// here's a snippet (some changes to protect our privacy). An improvement could be to create a jsonp model class which MyModel inherits
// the synch function is most important below, that's what tells backbone it's jsonp
MyModel = Backbone.Model.extend({
@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!');
@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 / 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 / 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 / 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 / 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)
})