[Proto-Scripty] Re: Form.serialize without empty inputs.
You can filter out the bogus values of the original or create a new object and only set the valid values. var data = $H(form.serialize(true)); var transportData = $H(); data.each(function(pair, itr) { if(pair[1] != "") transportData.set(pair[0], pair[1]); }); -- http://positionabsolute.net On Nov 8, 9:06 am, Braulio wrote: > Hello. > > On 7 nov, 04:39, "T.J. Crowder" wrote: > > > > > > > > > > > Hi, > > > `Form#serialize` returns a plain object (with a property for each form > > element, where the value of the property is the value of the element). > > Plain objects don't have an `#each` method. But for what you're trying > > to do, JavaScript itself has a built-in "each" mechanism: `for..in`. > > `for..in` enumerates the property names of an object. So you'd just > > use `for..in` and look for properties with "" as a value and delete > > them from the object: > > > var data, name; > > > // Get an object with the form data as properties > > data = $('step2').serialize(true); > > > // Loop through the form data > > for (name in data) { > > // Does this element have a blank value? > > if (data[name] === "") { > > // Yes, remove it from the data > > delete data[name]; > > } > > } > > > Here's an example:http://jsbin.com/icasa4(Theonly Prototype- > > specific code in the above is the `#serialize` call.) > > > You may be wondering if it's safe to modify the object's list of > > properties (via `delete`) when you're looping through that list (via > > `for..in`). It is, the spec[1] covers this in Section 12.6.4 ("The for- > > in Statement"): "Properties of the object being enumerated may be > > deleted during enumeration." > > > [1]http://www.ecma-international.org/publications/standards/Ecma-262.htm > > Thank you very much. Your answer was really helpful and enlightening. > > Best regards, > > B. > > > > > > > > > On Nov 5, 9:14 pm, Braulio wrote: > > > > Hello. > > > > Is it possible to serialize a form but without empty inputs? > > > > I've been looking on the Internet for an example and I have tried code > > > like this one, but without achieving success: > > > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) {$ > > > ('step2').remove(key)}}) > > > > or > > > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) > > > {this.remove(key)}}) > > > > Any help is appreciated. > > > > Best regards, > > > > B. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: Form.serialize without empty inputs.
Hello. On 7 nov, 04:39, "T.J. Crowder" wrote: > Hi, > > `Form#serialize` returns a plain object (with a property for each form > element, where the value of the property is the value of the element). > Plain objects don't have an `#each` method. But for what you're trying > to do, JavaScript itself has a built-in "each" mechanism: `for..in`. > `for..in` enumerates the property names of an object. So you'd just > use `for..in` and look for properties with "" as a value and delete > them from the object: > > var data, name; > > // Get an object with the form data as properties > data = $('step2').serialize(true); > > // Loop through the form data > for (name in data) { > // Does this element have a blank value? > if (data[name] === "") { > // Yes, remove it from the data > delete data[name]; > } > } > > Here's an example:http://jsbin.com/icasa4(The only Prototype- > specific code in the above is the `#serialize` call.) > > You may be wondering if it's safe to modify the object's list of > properties (via `delete`) when you're looping through that list (via > `for..in`). It is, the spec[1] covers this in Section 12.6.4 ("The for- > in Statement"): "Properties of the object being enumerated may be > deleted during enumeration." > > [1]http://www.ecma-international.org/publications/standards/Ecma-262.htm Thank you very much. Your answer was really helpful and enlightening. Best regards, B. > On Nov 5, 9:14 pm, Braulio wrote: > > > Hello. > > > Is it possible to serialize a form but without empty inputs? > > > I've been looking on the Internet for an example and I have tried code > > like this one, but without achieving success: > > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) {$ > > ('step2').remove(key)}}) > > > or > > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) > > {this.remove(key)}}) > > > Any help is appreciated. > > > Best regards, > > > B. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Re: Form.serialize without empty inputs.
Hi, `Form#serialize` returns a plain object (with a property for each form element, where the value of the property is the value of the element). Plain objects don't have an `#each` method. But for what you're trying to do, JavaScript itself has a built-in "each" mechanism: `for..in`. `for..in` enumerates the property names of an object. So you'd just use `for..in` and look for properties with "" as a value and delete them from the object: var data, name; // Get an object with the form data as properties data = $('step2').serialize(true); // Loop through the form data for (name in data) { // Does this element have a blank value? if (data[name] === "") { // Yes, remove it from the data delete data[name]; } } Here's an example: http://jsbin.com/icasa4 (The only Prototype- specific code in the above is the `#serialize` call.) You may be wondering if it's safe to modify the object's list of properties (via `delete`) when you're looping through that list (via `for..in`). It is, the spec[1] covers this in Section 12.6.4 ("The for- in Statement"): "Properties of the object being enumerated may be deleted during enumeration." [1] http://www.ecma-international.org/publications/standards/Ecma-262.htm HTH, -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Nov 5, 9:14 pm, Braulio wrote: > Hello. > > Is it possible to serialize a form but without empty inputs? > > I've been looking on the Internet for an example and I have tried code > like this one, but without achieving success: > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) {$ > ('step2').remove(key)}}) > > or > > $('step2').serialize(true).each(function(s, index) {if (s.empty()) > {this.remove(key)}}) > > Any help is appreciated. > > Best regards, > > B. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.