-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse-form.js
33 lines (24 loc) · 907 Bytes
/
parse-form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*global ParseForm:true */
ParseForm = function (elem) {
var self = this;
if (!elem) return console.error('You must provide an element or selector');
this.$el = $(elem);
this.el = this.$el[0];
// save each input into array, e.g. [{name: 'email', value:'[email protected]'}]
this.inputs = this.$el.serializeArray();
// itterate through all inputs and set their 'name' attr to the key & set the
// input value to the key's value. let's you call form.email to grab value.
for (var i = 0; i < this.inputs.length; i++) {
var input = this.inputs[i],
name = input.name,
$el = self.$el.find('[name="' + name + '"]');
// save input value, e.g. form.email >>> '[email protected]'
this[name] = $el.val();
// save jquery selector, e.g. form.$email
this['$' + name] = $el;
}
};
// reset/empty the form
ParseForm.prototype.reset = function () {
this.el.reset();
};