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


