How to get closest form input value in jQuery

Created at 19-Aug-2021 , By samar

How to get closest form input value in jQuery

With this article, we will examine several different instances of how to solve the "How to get closest form input value in jQuery".

You can get the closest form and input values of this form on click to submit button using closest method in jQuery
  • JQuery get closest form on click submit button

    <!-- HTML Form -->
    <form method="post" id="commentForm" action="/comment/store">
        <div class="form-group">
            <input type="text" class="form-control" name="body"/>
            <div class="invalid-feedback ErrorCommentBody"></div>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-success add-submit-btn" value="Add Comment" />
        </div>
    </form>
    
    <!-- CDN with javascript code -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script>
    $(document).on('click', '.add-submit-btn', function(e){
        e.preventDefault();
        let form = $(this).closest('form');
        console.log(form);
    });
    </script>
    

    This code snippet is used to get the closest form (related form) of the submit button.

  • JQuey get closest form and input fields value on click submit button

    <!-- Paste in <head> tag of your HTML page -->
    <!-- Bootstrap 4 and jQuery CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <p><strong>Past in body tag of HTML page</strong></p>
    //HTML form with javascript 
    <form method="post" id="commentForm" action="/comment/store">
        <div class="form-group">
            <input type="text" class="form-control" name="body"/>
            <div class="invalid-feedback ErrorCommentBody"></div>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-success add-submit-btn" value="Add Comment" />
        </div>
    </form>
    <div id="response"></div>
    
    <!-- Past in bottom of body tag of HTML page -->
    <script>
    $(document).on('click', '.add-submit-btn', function(e){
        e.preventDefault();
        let form = $(this).closest('form');
        let commentText = $.trim(form.find("input[name='body']").val());
        //Perform task 
        html = 'Value of input name body :'+ commentText;
        $('#response').append(html);
    });
    </script>
    

    This code snippet is used to target the form and get the input values of the form which is related to the submit button. If you have multiple forms on a page with (input fields) same name attributes and you want to get the value of the input fields related to this form then this code snippets will help you to do it.

Back to code snippet queries related jquery

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.