Working Ninja
2019-02-27T21:06:20

Take the following HTML snippet:

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li>Second to Last</li>
    <li></li>
</ul>

We would like to select the second to last list item (<li>). Here's how we'd go about doing that:

ul li:nth-last-child(2) { color: blue; }

Or, the inverse (everything but the second to last element):

ul li:not(:nth-last-child(2)) { color: red; }
2018-05-03T17:26:20

Toggle all <option>'s as checked/unchecked with the click of a <button>.

<select>
    <option></option>
    <option></option>
    <option></option>
</select>

<button></button>
jQuery("button").click(function(){
    jQuery("select :checkbox").each(function(){
        this.checked = !this.checked;
    });
});

Source: https://stackoverflow.com/questions/4177159/toggle-checkboxes-on-off

2017-07-19T10:46:37

Let's say you add an element to the DOM via jQuery:

$("<button class="delete">Delete</button>").appendTo("foo");

And want to later remove it via a jQuery event. Calling $('.delete').on('click') will not fire the event. This is because .on() only looks for elements that were loaded initially with the DOM. To resolve you can use .live() for older version of jQuery or if you're using later versions of jQuery (1.7+), the following:

$(document).on("click", ".delete", function() { ... });

Since "document" existed when the DOM loaded a click on it will fire an event. Furthermore, since we specified the ".delete" CSS class for the selector arguement for the .on() event, the event will search through its descendents to find any elements with the ".delete" CSS class and execute our function.

Sources:
https://stackoverflow.com/questions/4900802/jquery-click-not-working-after-adding-element-to-the-dom
http://api.jquery.com/on/

2017-02-16T15:41:13
#!/usr/bin/python
import sys


try: 
    hex_val = str(sys.argv[1])
except Exception as e:
    sys.exit('Please provide a hex code as an argument (e.g. hex2rgb 333333).')

r = int(hex_val[0:2], 16)
g = int(hex_val[2:4], 16)
b = int(hex_val[4:6], 16)

print('{}, {}, {}'.format(r, g, b))

Append your ~/.bash_aliases with:

alias hex2rgb="~/bin/hex2rgb.py"

Load the new alias:

$ source ~/.bash_aliases

Now we're able to convert Hex to RGB from the command line:

$ hex2rgb e1e1e1
255, 255, 255
2015-11-20T09:04:44
$( document ).ready(function() { });

is jQuery for:

document.addEventListener("DOMContentLoaded", function(event) { }); 
2015-10-14T08:12:31
$('#id').on('input', fuction(){
    var dInput = this.value;
    $('#output').text(dInput);
});
2014-11-03T17:55:25

There are times that you want to hide mobile specific features on desktop to avoid user confusion. Today I ran into such a case where a tel href on a phone number was being used that resulted in someone getting a 404. To hide this from desktop, I used jQuery to reference the display value to only load if the value was set to block.

var $isMobile = $(‘.toy-gallery td’).css(‘display’);

if ( $isMobile == ‘block’ ) {
    $(‘.phone-number’).wrapInner(‘<a href=”tel:5555555555″></a>’);
}