2

this code gives me an error when submitting he form "undefined is not a function in line 21" and I don't know why

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$("form[name=dForm]").submit(function(ev){
        ev.preventDefault();
        var data = $(this).serializeObject();
        data.push({name:"bonjour",value:bonjour});

 // do something here ...

})

or it must be an array not an object?

3
  • looks like data is an object and not an array.. Commented Sep 17, 2014 at 12:53
  • push is a Javascipt function. Not of jQuery Commented Sep 17, 2014 at 12:54
  • yes, and I dont know how to make it in array. because I have a multiple forms with same selector Commented Sep 17, 2014 at 12:55

1 Answer 1

3

serializeObject returns o which is defined at the top of the function as {}.

push is a method of Array objects, not plain objects. It puts an item onto the end of an array.

Plain objects are unordered. There isn't an "end" to put the item onto, so it doesn't make sense for there to be a push method.

You, presumably, want to just create a new property with a given name and value.

data.push({name:"bonjour",value:bonjour});

should be

data.bonjour = bonjour;
Sign up to request clarification or add additional context in comments.

1 Comment

@Shashank — What does that have to do with anything? This is about assigning a new value, not reading an existing one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.