Skip to content

Instantly share code, notes, and snippets.

View snowman-repos's full-sized avatar

James snowman-repos

View GitHub Profile
@snowman-repos
snowman-repos / Event Listeners
Created February 6, 2014 08:23
Event Listeners, without jQuery
element.addEventListener "click", ->
alert "You clicked"
, false
# Select all links
links = document.querySelectorAll("a")
# For each link element
[].forEach.call links, (el) ->
@snowman-repos
snowman-repos / HTML5 classList API
Created February 6, 2014 08:20
HTML5 classList API
if "classList" in document.documentElement
# do something
# Adding a class
element.classList.add "bar"
# Removing a class
element.classList.remove "foo"
# Checking if has a class
@snowman-repos
snowman-repos / Adding and Removing Classes
Created February 6, 2014 08:17
Adding and Removing Classes, without jQuery
# Select an element
element = document.querySelector(".some-class")
# Give class "foo" to the element
element.className = "foo"
# Adding a class without replacing the current classes
element.className += " foo"
# removeClass, takes two params: element and classname
@snowman-repos
snowman-repos / Traversing the DOM
Created February 6, 2014 08:11
Traversing the DOM, without jQuery
# Getting the parent node
parent = document.querySelector("div").parentNode
# Getting the next node
next = document.querySelector("div").nextSibling
# Getting the previous node
next = document.querySelector("div").previousSibling
# Getting the first child element
@snowman-repos
snowman-repos / Selectors API
Created February 6, 2014 08:10
Selectors API, without jQuery
element = document.querySelector "div"
elements = document.querySelectorAll ".container div"
navigation = document.querySelector "nav"
links = navigation.querySelectorAll "a"
# This gives us simple dollar function and event binding
$ = document.querySelectorAll.bind document
Element::on = Element::addEventListener
# This is how you use it
@snowman-repos
snowman-repos / DOM ready
Created February 6, 2014 08:07
Listen for DOM ready without jQuery
document.addEventListener "DOMContentLoaded", ->
# Code
, false
console.log('Hello World!');
//Hello World!
console.log('This is a string', { foo: 'bar' }, { bar: 'foo' });
//This is a string Object {foo: "bar"} Object {bar: "foo"}
var number = 11 * 9;
var color = 'red';
console.log('%d %s balloons', number, color);
//99 red balloons
@snowman-repos
snowman-repos / normalise.styl
Last active December 18, 2015 14:08
stylus version of normalise.css
// ! normalize.css v3.0.0 | MIT License | git.io/normalize
// 1. Set default font family to sans-serif.
// 2. Prevent iOS text size adjust after orientation change, without disabling
// user zoom.
html
font-family sans-serif
-ms-text-size-adjust 100%
@snowman-repos
snowman-repos / Stylus function for REM with fallback
Last active December 17, 2015 22:29
Stylus function for REM with fallback
x-rem ($property, $value)
{$property} $value * $main-font-size
{$property} {$value}rem
@snowman-repos
snowman-repos / Move cursor to the end of text input
Last active February 24, 2020 21:05
JavaScript: Move cursor to the end of text input
function moveCursorToEnd(el) {
if (typeof el.selectionStart == "number") {
el.selectionStart = el.selectionEnd = el.value.length;
} else if (typeof el.createTextRange != "undefined") {
el.focus();
var range = el.createTextRange();
range.collapse(false);
range.select();
}
}