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 <[email protected]> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.