2

I have the following setup:

<input name="myInput[3][0]" type="text">
<input name="myInput[3][1]" type="text">
<input name="myInput[4][0]" type="text">
<input name="myInput[4][1]" type="text">

With php it is pretty easy to access the values after the form has been submitted. For example using a "post"-method the values of the inputs are stored in the multidimensional array $_POST['myInput']. Then the first element in each dimension can be accessed with $_POST['myInput'][3][0].

Is there a similar functionality in JavaScript that can be used even without submitting a form via the "post"-method?

Following JavaScript - get value from multiple inputs in an array, I added a class called "myInput" to each input-element, like

<input name="myInput[3][0]" type="text" class="myInput">

Then I tried this:

var myInput= document.getElementsByClassName('myInput');

But, unsurprisingly, this gives me only a 1-dimensional array not respecting any of the array keys. So not exactly the result sought for. (Also it seems odd to fiddle around with classes.)

0

1 Answer 1

2

Following the example you provided, you can use 'match' function on the name of the input to get the indexed

m = inputs[i].name.match(/\[(\d+)\]\[(\d+)\]/);

A full working example is made available here (jsfiddle)

var inputs = document.getElementsByClassName( 'webcampics' ),
res  = {}, i;
for (i=0; i<inputs.length; i++){

    m = inputs[i].name.match(/\[(\d+)\]\[(\d+)\]/);
   if(!res[m[1]]){
       res[m[1]] = {};
    }
   res[m[1]][m[2] ]= inputs[i].value;

  }

 console.log(res);

As to using class as selector. If JQuery is used, an alternative is to wrap all input elements inside a div or form, give it an id (e.g. 'my_inputs'), then you can get the inputs like this:

var inputs = $('#my_inputs input');
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. That seems to work. However, as I mentioned in my post, I am still unsatisfied with the selection of the elements by class, since this seems to ruin the easiness of styling the the input-elements with css. Whenever I change the class I will have to check for both the css style and the JavaScript evaluation. This cannot really be the standard way to do this. Is there a way to select all the input elements only by using the names or so?
You can have multiple classes on an element. You can use one for as selector and the rest for styling. Using classes as selectors is very common, I don't think it is odd.
Maybe it is just because I am only used to using classes for css. It just seems strange to have this double naming: one for the name and one for the class, while all the information seems to be accessible already from the name alone.
Hi, if you use JQuery, there are handy ways to select the input elements without using 'class' (see my updated answer). Otherwise, using class is the easiest with pure JavaScript.
Okay, I guess you mean to wrap everything in a another element, e.g. a div with id="my_inputs".

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.