Skip to content

Instantly share code, notes, and snippets.

@fmartingr
Created August 21, 2013 14:03
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 fmartingr/6294877 to your computer and use it in GitHub Desktop.
Save fmartingr/6294877 to your computer and use it in GitHub Desktop.
Class to manage dynamic forms with coffeescript
class Form
###
Class to manage dynamic forms with javascript
> var form = Form('/destiny/path/', {'username':'fmartingr'})
> form.addField('password', 'aw8cnwn8t7x9my898wyf9mw67n')
> form.submit()
###
constructor: (action, params={}, method='post') ->
@form = document.createElement 'form'
@form.setAttribute 'method', method
@form.setAttribute 'action', action
for key in params
if params.hasOwnProperty key
@addField key, params[key]
addField: (key, value) ->
field = document.createElement 'input'
field.setAttribute 'type', 'hidden'
field.setAttribute 'name', key
field.setAttribute 'value', value
@form.appendChild field
submit: ->
document.body.appendChild @form # Not sure if neccesary
@form.submit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment