Skip to content

Instantly share code, notes, and snippets.

View jswhisperer's full-sized avatar
💭
zzz

Gregory The JSWhisperer jswhisperer

💭
zzz
View GitHub Profile
@jswhisperer
jswhisperer / Clear Form
Created June 18, 2013 16:21
Clear a form
function clearForm(form) {
// iterate over all of the inputs for the form
// element that was passed in
$(':input', form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
@jswhisperer
jswhisperer / enable submit
Created June 18, 2013 16:23
Enable Form Submit button if any text entered
$('#username').keyup(function() {
$('#submit').attr('disabled', !$('#username').val());
});
@jswhisperer
jswhisperer / highlight input field's label
Created June 18, 2013 16:24
Highlight an input field's label when selected
$("form :input").focus(function() {
$("label[for='" + this.id + "']").addClass("labelfocus");
}).blur(function() {
$("label").removeClass("labelfocus");
});
@jswhisperer
jswhisperer / urlshortener
Created June 18, 2013 16:25
URL shortener with google and coffeescript
apikey = "YOUR GOOGLE API KEY GOES HERE"
shorten_url = (url, success_callback, error_callback) ->
xhr = Titanium.Network.createHTTPClient()
xhr.open "POST", "https://www.googleapis.com/urlshortener/v1/url?key=" + apikey
xhr.setRequestHeader "Content-type", "application/json"
xhr.onload = () -> success_callback xhr.status, xhr.responseText
xhr.onerror = () -> error_callback xhr.status, xhr.responseText
content = "{\"longUrl\": \"#{url}\"}"
@jswhisperer
jswhisperer / Find substring with CoffeeScript
Created June 18, 2013 16:27
Find substring with CoffeeScript
message = "This is a test string. This has a repeat or two. This might even have a third."
message.indexOf "This", 0
# => 0
# Modifying the start parameter
message.indexOf "This", 5
# => 23
message.lastIndexOf "This"
# => 49
@jswhisperer
jswhisperer / Multiply each item in a list by 2
Created June 18, 2013 16:37
Multiply each item in a list by 2
i * 2 for i in [1..10]
@jswhisperer
jswhisperer / Sum a list of numbers
Created June 18, 2013 16:38
Sum a list of numbers
[1..1000].reduce (t, s) -> t + s
@jswhisperer
jswhisperer / Ajax in CoffeeScript
Created June 18, 2013 17:43
Ajax in CoffeeScript
$(document).ready ->
# Basic Examples
$.get '/', (data) ->
$('body').append "Successfully got the page."
$.post '/',
userName: 'John Doe'
favoriteFlavor: 'Mint'
(data) -> $('body').append "Successfully posted to the page."
@jswhisperer
jswhisperer / kill all mysql servers
Created June 19, 2013 16:35
kill all mysql servers reset mamp
sudo killall -9 mysqld
@jswhisperer
jswhisperer / getScript Ajax
Created June 23, 2013 14:19
getScript Ajax
// $.getScript()
var scriptUrl = "ajax/script.php";
$("#getScript").click(function(){
$("#result").html(ajax_load);
$.getScript(scriptUrl, function(){
$("#result").html("");
});
});