0

I have two forms in a html site and each form has an inputbox and a submit button

<div id="ausgabe">
 <form class="forms">
  <input type="text" placeholder="bla">
  <button type="submit" class="btn btn-primary btn-small">TestA</button>
 </form>
 <form class="forms">
  <input type="text" placeholder="bla">
  <button type="submit" class="btn btn-primary btn-small">TestB</button>
 </form>
</div>


$(".forms").submit(function(event)
{
    event.preventDefault(); // Prevent the form from submitting via the browser
   var test = $(this);
   var wert = test.parent().find(":input:first").val();
   alert("Button pressed"+wert); 
});

Then i type "TestA" in the first inputbox and press the submit-button for this form and get an "Button pressed TestA" alert.

Then i type "TestB" in the second inputbox which belongs to the second form and i expect this result "Button pressed TestB" but i get "Button pressed TestA" again

How can i get the value of the inputbox which belongs to the its form? My idea was to get the parent of the button which is pressed and find an inputbox there.

I'am generate this forms dynamicly from a java servlet backend and i not want to create ids for each form.

Best Regards rubiktubik

0

1 Answer 1

2

When targeting the form, you don't want to find the first input in the parent element, but in the form itself:

$(".forms").on('submit', function(event) {
    event.preventDefault();
    var test = $(this);
    var wert = test.find(":input:first").val();
    alert("Button pressed"+wert); 
});
Sign up to request clarification or add additional context in comments.

2 Comments

Damn I was just about to hit submit :(
You'll get it next time !

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.