1

I have a form that looks like the following:

<input type="text" name="item[101][0][date]" value="2012" />
<input type="text" name="item[101][0][price]" value="10.00" />
<input type="text" name="item[101][0][colour]" value="orange" />
<input type="text" name="item[101][1][date]" value="2013" />
<input type="text" name="item[101][1][price]" value="5.00" />
<input type="text" name="item[101][1][colour]" value="blue" />

And I would like to be able to select the entire array from the first 2 keys and store the whole lot as an array in javascript so I can send it via AJAX to PHP.

I've got this in jQuery at the moment, but it doesn't seem to produce what I'm after.

var products = $('input[name~="item['+itemCode+']['+basketId+']"]');

Where itemCode and basketId and retrieved from a submit button (which is working).

Once I have this array called "products" I know how to serialise it and send it as a post request, I just don't seem to be able to end up with an array such as:

products[101][0][date] = 2012
products[101][0][price] = 10.00
products[101][0][colour] = orange

Or:

products[101][1][date] = 2013
products[101][1][price] = 5.00
products[101][1][colour] = blue

Any help with this would be appreciated!

Thanks!

2 Answers 2

2

I would suggest using data-* HTML5 attributes to store data at different holders instead of everything stored in the name. Something like:

<input type="text" data-category="101" data-index="0" data-id="date" value="2012" />

Selection then would be performed the following way:

var input = $('input[data-category=' + category + '][data-index=' + index + '][data-id=' + id + ']');

This also allows you to select all items of the same category easily, or with the same index, etc.

The array would be constructed like:

var products = [];
// Define category, index and id
products[category][index][id] = $('input[data-category=' + category + '][data-index=' + index + '][data-id=' + id + ']').val();
Sign up to request clarification or add additional context in comments.

4 Comments

If I use this method, can you give me an example of how I'd select all the inputs based on their data-itemcode and data-basketId in JQuery then?
I updated the example with how you would select based on the data attributes.
Ok. Once I have "input" (or "products" as I've called it). How do I end up with an array as per the last section of my question above? If I attempt an "alert(products[date]);" or "alert(products[101][0][date]);" I don't get any output!
I see you've added the part about the products array. I would like iterate through all instances of itemCode and basketId, without me knowing what the 3rd index is. Is there anyway of doing this?
0

You are using the attribute contains word selector (name~=value), which does a slightly different thing from what you want:

This selector matches the test string against each word in the attribute value, where a "word" is defined as a string delimited by whitespace. The selector matches if the test string is exactly equal to any of the words.

Use the attribute starts with selector (name^=value):

var products = $('input[name^="item['+itemCode+']['+basketId+']"]');

Either use the attribute contains selector (name*=value):

var products = $('input[name*="item['+itemCode+']['+basketId+']"]');

DEMO.

6 Comments

That doesn't seem to work at all as per my question (tried with ^ and ~). When I try "alert(products[101][0][date]);" I get nothing at all!
@AndyBarlow: You need to take the .val() of each element.
Can you give me an example? I really want to end up with just a plain boring javascript array of products selected by their keys.
@AndyBarlow: Can't you modify your HTML to use data-* attributes for example? It's not easy and it's very error prone to store a multi-dimensional array in an attribute. I'll put an example, but please consider changing your HTML structure.
I can probably change my HTML if I absolutely must. If you could change your demo fiddle to include the method I was intending to use I'd be hugely appreciative.
|

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.