Cannon Moyer
Cannon Moyer

Reputation: 3164

Get full URI of Form Action - JavaScript

I've got the following form:

<form action="/action_page.php" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

Once the user hits submit, the full URL will look like this: example.com/action_page.php?fname=John&lname=Doe. I want to be able to grab the full URL of the form like you would if you called document.getElementById("form").action;

Is there a native method in either JavaScript or jQuery that will allow me to grab this URI? I'm wanting to save the full URL in a cookie. I know that I could technically parse each input value and build a string but I was wondering if there was a more simple way about to do this.

Thanks in advance.

Upvotes: 0

Views: 446

Answers (1)

Leeish
Leeish

Reputation: 5213

var href = $('form').attr('action')+ '?' +$('form').serialize();

Is this what you need? There may be some additional work for checkbox/radio field types.

Upvotes: 1

Related Questions