Skip to content

Instantly share code, notes, and snippets.

@MikeMcChillin
Last active December 15, 2015 22:29
Show Gist options
  • Save MikeMcChillin/5333820 to your computer and use it in GitHub Desktop.
Save MikeMcChillin/5333820 to your computer and use it in GitHub Desktop.
#######################
# Email subscribe
#######################
form = $("#subscribe")
form.submit (e) ->
e.preventDefault()
form.removeClass('shake')
$.getJSON @action + "?callback=?", $(this).serialize(), (data) ->
messageNode = $('.message')
# if there's an error...
if data.Status is 400
invalidSubmit = $("#{form} input[type=email]").val()
form.addClass('shake')
messageNode.html("Sorry, something went wrong with the email address #{invalidSubmit}.")
else
justSubmitted = $("#{form} input[type=email]").val()
successEntry = "Thank You!"
messageNode.html("Welcome to the mailing list!")
if $('.email-added').length > 0
$('.email-added').replaceWith(successEntry)
else
$('form input[type=submit]').val(successEntry)
setTimeout ( ->
$('form input[type=submit]').val('Submit')
), 1500
<form id="subscribe">
<input type="email" />
<input type="submit" />
</form>
<p class="message"></p>
form
::-webkit-input-placeholder
::-moz-placeholder
:-ms-input-placeholder
input[type="email"]
&:focus
input[type="submit"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment