Brave Type Brave Type - 3 months ago 9
HTML Question

Add input into form with jquery and submit

I want to add an input into form using JQuery, and then submit the form with pure HTML without using javascript.

<form method="post" action="" id="myform" >
<input type="hidden" name="example" value="done" />
<input type="submit" value="try" />
</form>


Here my JQuery line that add new input:

$('#myform').append("<input name="example2" value="done" type='hidden'/>");


My issue is when i submit the form, i can't get
example2
input value ! help me to figure it out.

Answer

What you need to do is download jquery library or use the external google cdn library as the one i have used in my case in the head of your html file.

Ensure that you append the input on the form as the document loads. If you intend to submit your form to Php as the server side check if the form is posted and print the output. You should save your file with .php extension.

Try the solution below. Thanks

$(document).ready(function() {
        $("form#myform").append("<input name='example2' value='done' type='hidden'/>")
      })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <form method="post" action="" id="myform">
      <input type="hidden" name="example" value="done" />
      <input type="submit" value="try" />
    </form>