Skip to content

Instantly share code, notes, and snippets.

View Mottie's full-sized avatar
💭
🥞

Rob Garrison Mottie

💭
🥞
View GitHub Profile
@Mottie
Mottie / gist:461488
Last active February 5, 2023 22:04
Improved jQuery :contains() selector
/* jQuery selector to match exact text inside an element
* http://wowmotty.blogspot.com/2010/05/jquery-selectors-adding-contains-exact.html
* :containsExact() - case insensitive
* :containsExactCase() - case sensitive
* :containsRegex() - set by user ( use: $(el).find(':containsRegex("/(red|blue|yellow)/gi")') )
*/
$.extend( $.expr[":"], {
containsExact: $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elem) {
@Mottie
Mottie / gist:461493
Created July 2, 2010 15:17
Return Hex color from RGB format
/* Javascript function to convert rgb format to a hex color
* call : rgb2hex( "rgb(0, 70, 255)" );
* returns: #0046ff
*/
function rgb2hex(rgb){
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2);
@Mottie
Mottie / gist:461498
Created July 2, 2010 15:19
Return URL Paramater Value
/* Returns URL parameter
* url: http://www.somesite.com?name=hello&id=11111
* z = gup('name'); // z = hello; s = string, or leave blank to target window location
* Original code from Netlobo.com (http://www.netlobo.com/url_query_string_javascript.html)
*/
function gup(n,s){
n = n.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var p = (new RegExp("[\\?&]"+n+"=([^&#]*)")).exec(s || window.location.href);
return (p===null) ? "" : p[1];
}
@Mottie
Mottie / gist:461516
Created July 2, 2010 15:30
Return Unique Array
/* Return a unique array
* myArray = ["ccc","aaa","bbb","aaa"];
* x = myArray.getUnique(); // x = ["ccc","aaa","bbb"]
* y = myArray.getUnique(true); // y = ["aaa","bbb","ccc"]
*/
// Slightly faster version than getUnique2; Modified from
// http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
Array.prototype.getUnique1 = function(s){
var c, a = [], o = {}, i, j = 0, l = this.length;
for(i=0; i<l; ++i){
@Mottie
Mottie / gist:642431
Created October 23, 2010 16:52
Return Duplicates from Array
/* Find & return only duplicates from an Array
* also returned is an object with the # of duplicates found
* myArray = ["ccc", "aaa", "bbb", "aaa", "aaa", "aaa", "aaa", "bbb"];
* x = myArray.getDuplicates();
* // x = [ array of duplicates, associative object with # found]
* // x = [ ['aaa','bbb'] , { 'aaa' : 5, 'bbb' : 2 } ]
* alert(x[0]) // x[0] = ['aaa','bbb'] & alerts aaa,bbb
* alert(x[1]['aaa']) // alerts 5;
*/
Array.prototype.getDuplicates = function(sort) {
@Mottie
Mottie / jQuery Enable-Disable
Last active September 25, 2015 10:18
Enable or disable objects using jQuery
/* Enable or disable DOM objects
* This is a basic script - it won't determine
* if an object can be enabled or disabled
*
* Use: $('button').disable();
* $('button').enable();
*/
(function($){
$.fn.extend( {
enable : function(){
@Mottie
Mottie / gist:938670
Created April 23, 2011 14:58
Cycle Through Visual Event Bookmarklet Layers
// Code I paste into the Firebug Console after I run the Visual Event
// bookmarklet (http://www.sprymedia.co.uk/article/Visual+Event)
// so I can cycle through the layers - Use arrow keys to cycle
var veColors = [ 'black', 'orange', 'purple', 'green', 'blue', 'yellow', 'red' ],
veColorLength= veColors.length - 1,
veLayerIndex = 0;
function showVeLayer(nxt){
veLayerIndex += (nxt) ? 1 : -1;
@Mottie
Mottie / gist:1079524
Created July 13, 2011 00:55
Adding WordPress Quicktag Buttons to a WP Plugin
<?php
/************************************************
Adding WordPress Quicktag Buttons to a WP Plugin
************************************************/
?>
<style>
/* Simulate the input buttons styles */
#ed_toolbar button {
display: inline-block;
vertical-align: middle;
@Mottie
Mottie / gist:1301303
Created October 20, 2011 14:34
Add Swipe Support
// http://wowmotty.blogspot.com/2011/10/adding-swipe-support.html
// **********
var target = $('#box'),
// allow movement if < 1000 ms (1 sec)
maxTime = 1000,
// swipe movement of 50 pixels triggers the swipe
maxDistance = 50,
@Mottie
Mottie / gist:1301306
Created October 20, 2011 14:35
Add jQuery renameAttr
// http://wowmotty.blogspot.com/2011/10/jquery-rename-attribute-renameattr.html
// *********
jQuery.fn.extend({
renameAttr: function( name, newName, removeData ) {
var val;
return this.each(function() {
val = jQuery.attr( this, name );
jQuery.attr( this, newName, val );
jQuery.removeAttr( this, name );
// remove original data