How to capture form input into objects

Features :
1. auto check for selected and non-selected checkbox
2. auto capture selected radio


function nvp2obj(selector) {
var inputs = $(selector);
var o = {};
inputs.each(function(i) {
if (this.name) {
if (this.type == 'checkbox' && $(this).attr('checked')) o[this.name] = $(this).val();
if (this.type == 'radio' && $(this).attr('checked')) o[this.name] = $(this).val();
if (this.type == 'text' || this.type == 'textarea') o[this.name] = $(this).val();
}
});
return o;
}

How to use :
$('#btnLetsTalk').click(function(e) {

var inputs = nvp2obj('#frmLetsTalk :input');

console.log(inputs);

e.preventDefault();

});

Advertisement

About kaixersoft

Proud to be Pinoy. Batangueno in nature. Love to see and write computer codes. View all posts by kaixersoft

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.