OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get data for form input array using specific key

  • Thread starter Thread starter Pete
  • Start date Start date
P

Pete

Guest
So, let's say I have an HTML form like this:

Code:
<form id="myForm">
    <input type="text" name="dummy">
    <input type="text" name="people[0][first_name]" value="John">
    <input type="text" name="people[0][last_name]" value="Doe">
    <input type="text" name="people[1][first_name]" value="Jane">
    <input type="text" name="people[1][last_name]" value="Smith">
</form>

And I want to get a JavaScript array that matches the values of real. For example:

Code:
// If there was a sweet function for this...
var people = getFormDataByInputName( 'people' );

// Value of `people` is...
// [
//    {
//        'first_name' : 'John',
//        'last_name'  : 'Doe'
//    },
//    {
//        'first_name' : 'Jane',
//        'last_name'  : 'Smith'
//    }
// ]

Is there any easy way of doing that for just a specific form item (in this case, people)? Or would I have to serialize the entire form an then just extract the element I want?

I also thought of potentially using the following approach:

Code:
var formData = new FormData( document.querySelector( '#myForm' ) );
var people = formData.get( 'people' );

But that doesn't appear to work; people is just null after that.

<p>So, let's say I have an HTML form like this:</p>

<pre><code><form id="myForm">
<input type="text" name="dummy">
<input type="text" name="people[0][first_name]" value="John">
<input type="text" name="people[0][last_name]" value="Doe">
<input type="text" name="people[1][first_name]" value="Jane">
<input type="text" name="people[1][last_name]" value="Smith">
</form>
</code></pre>

<p>And I want to get a JavaScript array that matches the values of <code>real</code>. For example:</p>

<pre><code>// If there was a sweet function for this...
var people = getFormDataByInputName( 'people' );

// Value of `people` is...
// [
// {
// 'first_name' : 'John',
// 'last_name' : 'Doe'
// },
// {
// 'first_name' : 'Jane',
// 'last_name' : 'Smith'
// }
// ]
</code></pre>

<p>Is there any easy way of doing that for just a specific form item (in this case, <code>people</code>)? Or would I have to serialize the entire form an then just extract the element I want?</p>

<p>I also thought of potentially using the following approach:</p>

<pre><code>var formData = new FormData( document.querySelector( '#myForm' ) );
var people = formData.get( 'people' );
</code></pre>

<p>But that doesn't appear to work; people is just null after that.</p>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top