user1687763
user1687763

Reputation: 11

Javascript not finding first form

I am struggling with a javascript/jquery issue. I have multiple forms that are created within a foreach loop and multiple javascript functions that are created in a foreach loop.

Essentially, the first form looks like this:

<form role="form" action="/save/parishioner" id="1" name="1" method="post">
<input type="text"  class="form-control" name="acct_number" id="acct_number" value="test" onchange="save1()" />
<input type="text"  class="form-control" name="first_name" id="first_name" value="name" onchange="save1()"/>
</form>

The second form looks like this:

<form role="form" action="/save/parishioner" id="2" name="2" method="post">
<input type="text"  class="form-control" name="acct_number" id="acct_number" value="test" onchange="save2()" />
<input type="text"  class="form-control" name="first_name" id="first_name" value="name" onchange="save2()"/>
</form>

And the functions looks like this:

alert($('form[name="2"]').serialize());
alert($('form[name="3"]').serialize());

When I try to serialize the data and alert it, the first alert returns blank and the second alert shows the second form serialized.

Why is it not finding the first form?

I would greatly appreciate any help!

Upvotes: 0

Views: 59

Answers (1)

danMontague
danMontague

Reputation: 71

From the code you have posted (unless this is a mistake in your post) you are actually getting a serialized alert from the first call not the second. Your selectors in the alert are wrong

alert($('form[name="2"]').serialize());
alert($('form[name="3"]').serialize());

so the values you are placing in name param of the form and then in the selector in your alert do not match. They should be:

alert($('form[name="1"]').serialize());
alert($('form[name="2"]').serialize());

Upvotes: 1

Related Questions