0

I have an array called 'inputs' that I want to push some values to.

I am wondering if this is the correct way to do so:

inputs.push(name: $('#uploaderName').name(), value: $('#uploaderName').val());

#uploaderName is the ID of a form field with the value I want pushed.

FYI, the 'inputs' array is created like this:

var inputs = data.context.find(':input').not(':button');

I have also tried this with no luck:

inputs.push({name: $('#uploaderName').attr("name"), value: $('#uploaderName').val()});

To explain a bit more, at the end of this script it calls:

data.formData = inputs.serializeArray();

and then that data is passed off to a PHP script. I need to be able to add a few values to 'inputs' before inputs.serializeArray() is called.

9
  • No, because you're pushing an object. Commented Mar 8, 2014 at 6:42
  • inputs is made up of some field names and the values, I just need to be able to add a few more name/value pairs to the already generated 'inputs' variable. Commented Mar 8, 2014 at 6:56
  • Are you using a library like jQuery or Zepto? .find(':input').not(':button') to me suggests that data.context and inputs will be array-like Element collections rather than actual Arrays. If so, they won't have .push() methods. But, you can get an Array from them with .get(). Commented Mar 8, 2014 at 7:02
  • The script I am working with uses jQuery Commented Mar 8, 2014 at 7:03
  • I also added a bit more info to my question Commented Mar 8, 2014 at 7:05

2 Answers 2

1

For jQuery collections, you can include additional Elements with .add().

inputs = inputs.add('#uploaderName');

jQuery collections aren't actually Arrays. They're just array-like, meaning they have a length and numeric properties, but they won't have Array methods like .push().

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't seem to work. Is there any way to tell what type of data 'inputs' is so we know how to properly add to it?
@SherwinFlight Sorry. Just updated the snippet. .add() creates a revised collection rather than mutating the original, so you have to store the result.
Should it be '#uploaderName' like you have or $('#uploaderName')?
@SherwinFlight Both should work. .add() can accept either a selector or a jQuery object.
The way you had it in your answer worked fine. I am accepting your answer. Thanks for the help :)
0

You are storing the object, so use {...} to make the object first, and store it into inputs array, like

inputs.push({name: $('#uploaderName').name(), value: $('#uploaderName').val()});
//----------^----------------------------------------------------------------^

Or

var myObj  = {name: $('#uploaderName').name(), value: $('#uploaderName').val()}; 
inputs.push(myObj);

1 Comment

This doesn't seem to work, but I added some info to my question

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.