0

I have a form to add many contacts in html the input is two dimensional array here is the form:

<input class="form-control" type="text" name="account[0][name]" placeholder="Name"  autocomplete="off" />
<input class="form-control" type="text" name="account[0][email]" placeholder="Email"  autocomplete="off" />
<input class="form-control" type="text" name="account[0][mobile]" placeholder="Mobile"  autocomplete="off" />

<input class="form-control" type="text" name="account[1][name]" placeholder="Name"  autocomplete="off" />
<input class="form-control" type="text" name="account[1][email]" placeholder="Email"  autocomplete="off" />
<input class="form-control" type="text" name="account[1][mobile]" placeholder="Mobile"  autocomplete="off" />

I am trying to get the value using jqueryI tried the following, it returns the value but i want to return an input value of account name or account email or account mobile

 $('input[name^="account"]').each(function() {
                console.log($(this).val());
        });
4
  • What do you mean "return an exact input"? You want what's in the string in the second brackets? Commented Feb 29, 2016 at 9:41
  • I meant like to return the value of account name or account email or account mobile Commented Feb 29, 2016 at 9:43
  • If you want the value of the input, meaning what the user inputs, then your code works. Please be more specific. Commented Feb 29, 2016 at 9:44
  • I want to get the value of account[0][name], account[1][name] , account[2][name] , account[3][name] , account[4][name] Commented Feb 29, 2016 at 9:46

3 Answers 3

1

That is not value that is an attribute called placeholder so you should use .attr() method instead:
console.log($(this).attr('placeholder'));

but i want to return an input value of account name or account email or account mobile for this you can use .serializeArray();

$('pre').html(JSON.stringify($('.form-control').serializeArray(), 0, 3));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>

<input class="form-control" type="text" name="account[0][name]" placeholder="Name"  autocomplete="off" />
<input class="form-control" type="text" name="account[0][email]" placeholder="Email"  autocomplete="off" />
<input class="form-control" type="text" name="account[0][mobile]" placeholder="Mobile"  autocomplete="off" />

<input class="form-control" type="text" name="account[1][name]" placeholder="Name"  autocomplete="off" />
<input class="form-control" type="text" name="account[1][email]" placeholder="Email"  autocomplete="off" />
<input class="form-control" type="text" name="account[1][mobile]" placeholder="Mobile"  autocomplete="off" />

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

Comments

1

You can use the starts with and ends with attribute selectors:

$('[name^="account"][name$="[name]"]')

Would select all elements where the name starts with account and ends with [name].

Comments

0

Try this.

$('input[name^="account"]').each(function() {
  alert($(this).val());
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input class="form-control" type="text" value="test" name="account[0][name]" placeholder="Name" autocomplete="off" />
<input class="form-control" type="text" value="[email protected]" name="account[0][email]" placeholder="Email" autocomplete="off" />
<input class="form-control" type="text" value="1234568790" name="account[0][mobile]" placeholder="Mobile" autocomplete="off" />

<input class="form-control" type="text" value="test2" name="account[1][name]" placeholder="Name" autocomplete="off" />
<input class="form-control" type="text" value="[email protected]" name="account[1][email]" placeholder="Email" autocomplete="off" />
<input class="form-control" type="text" value="3216549870" name="account[1][mobile]" placeholder="Mobile" autocomplete="off" />

Comments

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.