0

I want to change my form destination based on an input value. How can I do this?

<form id="form" method="POST" action="x.php" onSubmit="">
  <input type="url" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>

Example: When I set http://google.com as address value, so send this form to google.com.

Amin Maleki
  • 156
  • 2
  • 4
  • 19

2 Answers2

1
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

 <script type="text/javascript">
            $(document).ready(function () {
                $("#address").blur(function () {
                    var form = document.getElementById('form');
                        form.action = $("#address").val();
                });
            });
</script>

<form id="form" method="POST" action="x.php" onSubmit="">
  <input type="text" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>
Prashant Valanda
  • 480
  • 8
  • 19
1

I use "How do I get the value of text input field using JavaScript?" & "How can I set the form action through JavaScript?" to write my code:

<script type="text/javascript">
    function get_action(form) {
        form.action = 'http://'+document.getElementById("address").value;
    }
</script>

<form onsubmit="get_action(this);">
  <input type="text" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>
Amin Maleki
  • 156
  • 2
  • 4
  • 19