0

i get all the forms generated by a PHP while loop so i can not change their id and classes..they all come in the DOM with same id and class but every form input has their own distinct value ...all i want is,,if i click on submit of a particular form i want the jquery to get the input value only of that specific form .

   <form  class ='myform' >
     <input type = "hidden" name ="fname" value = "sam">
     <input type = "submit" value = "Submit">
   </form>


    <form class = 'myform'>
      <input type = "hidden" name = "fname" value = "olivia">
      <input type = "submit" value = "Submit">
    </form>


    <form class = 'myform'>
      <input type = "hidden" name = " fname " value = "justien">
      <input type = "submit" value = "Submit">
    </form>

   <scirpt >

    ----here i want the solution
   </script>
pr7
  • 9
  • 2
  • Does this answer your question? [How to get the children of the $(this) selector?](https://stackoverflow.com/questions/306583/how-to-get-the-children-of-the-this-selector) – Alon Eitan Apr 14 '20 at 10:47

2 Answers2

0

You could either add a counter into the loop statement for creating the myForm.

`<form class = 'myform${counter}'>`

This would make the forms have a unique class but would make CSS difficult. Alternatively, you could select the parent of the input field

$('input[value="justien"]').parent();

That should work

velez_dot
  • 122
  • 6
  • i tried to add the counter and pass the total number to jquery and tried to do while loop if the form is clicked then only get the children but .submit do not work good with loop – pr7 Apr 14 '20 at 11:10
  • not exactly ,,,,i solved it as i posted below ,,,answered by me ...username pr7--- – pr7 Apr 15 '20 at 09:25
0

It works for me like a charm

$(document).ready(
    $(".myforms']").submit(function(e) {
        e.preventDefault();
        var f_name = $(this).children("input[name='f_name']");
        alert(f_name.val());
    })
);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
pr7
  • 9
  • 2