Home > OS >  How to populate the value of hidden form input with the window location URL
How to populate the value of hidden form input with the window location URL

Time:04-08

My form is currently set up so it grabs the value of a hidden input with name="url". Is there a script I can use for it to recognize the window URL and post it to my form?

input[type="hidden"] {
  display: none;
}
<form>
  <!-- form elements -->
  <input type="hidden" name="url">
</form>

CodePudding user response:

Use QuerySelector('input[name=url]') then use .value for it to populate in the value field with the window location href.

document.QuerySelector('input[name=url]').value = window.location.href;

.js with HTML here:

document.QuerySelector('input[name=url]').value = window.location.href;
<input type="hidden" name="url">
  • Related