Created
August 21, 2013 14:03
-
-
Save fmartingr/6294877 to your computer and use it in GitHub Desktop.
Class to manage dynamic forms with coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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