How to add more data into an exisitng data using jquery?

72 Views Asked by At

How can I add more data into an exisitng data using jquery - or just plain javascript?

fd = {
    name : file_data.name,
    size : file_data.size
};

// And others.
$("form").find('input[type=hidden]').each(function(){
    $(fd).data(this.name, this.value);
});

I get this strange result,

Array
(
    [name] => Dangerous Man.mp3
    [size] => 11912748
    [jQuery1910560199169432603] => Array
        (
            [toJSON] => undefined
            [data] => Array
                (
                    [article_id] => 
                    [category_id] => 15
                    [user_id] => 1
                    [max_upload_filesize] => 3
                    [method] => upload
                    [required[category_id] => Category ID
                )

        )

)

But I am after this result,

Array
(
    [name] => Dangerous Man.mp3
    [size] => 11912748
    [article_id] => 1
    [category_id] => 15
    [user_id] => 1
    [max_upload_filesize] => 3
    [method] => upload
    [required[category_id] => Category ID

)

any ideas?

2

There are 2 best solutions below

0
Alcadur On BEST ANSWER

Just add property to object

$("form").find('input[type=hidden]').each(function(){
    fd[this.name] = this.value;
});
2
Vlas Bashynskyi On

You can pass only Element, NodeList, selector or a function into $ function, not an Object. You shoud do it this way :

fb[ this.name ] = this.value;

instead of

$(fd).data(this.name, this.value);