html - jQuery create a real-time array from multiple inputs with the same class -
i have multiple inputs on page have same class name authority-email. using jquery values inputs using following:
var emailobj = {}; $("input[class=authority-email]").each(function () { var email = $(this).val() emailobj = email; console.log(emailobj); }); these inputs can removed , added dom using jquery. values within inputs editable.
as input changes (remove, add, edit) best way pass values in real-time emailobj?
your current code changing emailobj object string on each iteration of loop, instead of amending property of object itself. note can use . style selector match elements class.
to achieve require, can use map() create array group of elements in jquery object. can assign required property of emailobj object. example:
var emailobj = {}; emailobj.emails = $("input.authority-email").map(function () { return this.value; }); console.log(emailobj.emails); // = [ 'a@a.com', 'b@b.com', ... ] to update object in 'real-time', hook change , keyup events of inputs themselves:
var emailobj = {}; $("input.authority-email").on('change keyup', function() { emailobj.emails = $("input.authority-email").map(function () { return this.value; }); console.log(emailobj.emails); // = [ 'a@a.com', 'b@b.com', ... ] });
Comments
Post a Comment