1

Script here:

$('#txt').click(function(){

         $('#formID').attr('action', 'https://www.google.se/');

});

Html here:

<form method="post" name="myForm" action="http://www.youtube.com/" id="formID"  >

        <input type="button" id="txt" value="Submit" />

</form>

So basically what I want to happen is that when I press the submit button the action of the form will be changed to google and google will open. This is not working at all and I dont know why. :|

user2722885
  • 43
  • 1
  • 5

4 Answers4

2

This is not really what forms were designed to do, so you must override the normal behavior of the submit button in your form.

One way to do this is via jQuery (assuming txt is the id of your submit button):

$('#txt').click(function(e) {
    e.preventDefault(); //prevent form from attempting to submit
    window.location.href = "http://google.com";
});

Note: updated. Forgot the 'e' inside the function() and parens after e.preventDefault()

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • If `txt` is the ID of your form, then it should work. Are you sure that you have the correct ID for your form? What is the exact `
    – cssyphus Aug 27 '13 at 21:09
  • As it says in my HTML above, #txt is the ID of my button and the ID for my form is #formID – user2722885 Aug 27 '13 at 21:12
  • Right, sorry, that was what I meant. Well, this should work for you... but if it doesn't, please try j08691's answer. It will do *exactly* what you want. So now you should have two methods to accomplish this. Please let us know if you continue to have problems... – cssyphus Aug 27 '13 at 21:13
  • I've tried both of yours and written them exactly as you did but nothin happens at all. I can't even manage to open youtube by pressing my button without the script – user2722885 Aug 27 '13 at 21:16
  • Then start over with a clean page. I will post the EXACT code from my local example in another answer. – cssyphus Aug 27 '13 at 21:17
2

Try:

$('#txt').click(function (e) {
    e.preventDefault();
    $('#formID').attr('action', 'https://www.google.se/').submit();
});
j08691
  • 204,283
  • 31
  • 260
  • 272
1

This is the exact code that worked for me. Copy/Paste it into a new document and give it a try. You can see where I commented out my own answer to try j08691's answer (it also worked).

Let us know.

<html>
    <head>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

        <script type="text/javascript">

            $(document).ready(function() {

                $('#txt').click(function(e) {
                    e.preventDefault();
                    //window.location.href = "http://google.ca";
                    $('#test').attr('action', 'https://www.google.se/').submit();
                });
            }); //END $(document).ready()

        </script>
    </head>

<body>

    <form id="test">
        Fisrt: <input type="text" />
        Last : <input type="text" />
        <input id="txt" type="submit" value="Go" />
    </form>

</body>
</html>
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • The button doesn't open google but rather seem to reload itself. – user2722885 Aug 27 '13 at 21:23
  • Okay, I'm at a loss. The above works on my system, opening google. What browser are you using (I tested with latest Google Chrome). Also, try using `window.open`, like [here](http://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript) – cssyphus Aug 27 '13 at 21:28
  • OK this seems to be working now. All I did was changing the jquery version to 1.10.2 instead of what you wrote. – user2722885 Aug 27 '13 at 21:29
  • Okay, very happy to hear it. Please upvote any useful posts and select a correct answer when you are satisfied. – cssyphus Aug 27 '13 at 21:41
  • I have too low of a reputation to be allowed to upvote people apparently. But I'll select yours as the correct answer since you beared with me till it worked! – user2722885 Aug 27 '13 at 21:47
  • Thank you. If you have any further questions, just post another one. – cssyphus Aug 27 '13 at 21:51
0

The problem is that your button isn't a submit button, so even now your code won't do a thing unless you have some other JavaScript or jQuery you've not shown us or you left out your submit button?

Anyway, this is the new jQuery:

$(function() { //Run on page load
    $('#txt').click(function(e) {
        e.preventDefault(); //Prevent default action from happening
        $('#formID').attr('action', 'https://www.google.se/').submit(); //Change the form's action attribute
        $('#formID').submit(); //Submit the form
    });
});

Html here - changed type="button" to type="submit" and changed method="post" to method="get" because google doesn't like post:

<form method="get" name="myForm" action="http://www.youtube.com/" id="formID"  >
    <input type="submit" id="txt" value="Submit" />
</form>
2xAA
  • 308
  • 3
  • 11