javascriptjqueryform-submitsubmit-button

How to get input's value with jQuery and submit it


I need to execute jQuery code when the submit button is pressed.

The jQuery get the value of a input field so the submit button can send it with the rest of the form.

This is my HTML code (submit button and input field):

<input type="hidden" id="myInput" name="myInput">

<button type="submit" id="mySubmit">Send</button>

This is my jQuery code:

$(document).ready(function() {
    $("#mySubmit").click(function() {
        $("#myInputField").val("Hello");
    }) 
})

Solution

  • $('#myform').submit(function(e) {
        $("#myInput").val("Hello");
    });
    <form id="myform">
    <input type="hidden" id="myInput" name="myInput">
    
    <button type="submit" id="mySubmit">Send</button>
    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>