How to change the form /action dynamically using jquery - jQuery 1


We can simply set the form action with the page load using jQuery. Below is an example of it.




<html>
<head>
    <meta charset="UTF-8">
    <title>Change Form Action with jQuery</title>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>

    <form id="myForm" action="/action" method="post">
        <label for="name">Name: </label>
        <input type="text" id="name" name="name">
        <button type="submit">Submit</button>
    </form>

    <script>        
        $(document).ready(function () {
            $('#myForm').attr('action', '/new-action');
        });
    </script>

</body>
</html>

Here myForm is the form ID, which sets the action attribute with the page load.


Inspect in browser - 





Post a Comment

0 Comments