Skip to content

Instantly share code, notes, and snippets.

@Arcolye
Last active January 26, 2017 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arcolye/0dfa2fdde8e9f9db24cf09de5b429909 to your computer and use it in GitHub Desktop.
Save Arcolye/0dfa2fdde8e9f9db24cf09de5b429909 to your computer and use it in GitHub Desktop.
GoToStudy - Photo upload
# Selecting a photo to upload shows photo and filename instantly
placeSelectedPhotoPreview = (input) ->
if input.files && input.files[0]
reader = new FileReader()
reader.readAsDataURL(input.files[0])
reader.onload = (e) ->
$(input).closest('.thumbnail').find('img#photo-preview').attr("src", e.target.result)
$(input).closest('.thumbnail').find("#upload-filename").html(input.files[0].name)
# When a photo is selected
$(document).on 'change', '.photo-inputs input', -> placeSelectedPhotoPreview(this)
# Clicking the photo preview also activates file_field input.
$(document).on 'click', '.photo-inputs img#photo-preview', ->
$(this).next('div').find('a.btn input').click()
# Add a new photo selection field when you select a photo.
# Only for .photo-inputs-auto_add_field
$(document).on 'change', '.photo-inputs-auto_add_field input', ->
$(this).closest('.photo-inputs-auto_add_field').find('a.add_fields').click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment