0

Here's what I'm essentially trying to do:

<input type="text" class="form-control" value="window.location.hash">

What's the proper way to insert the window.location.hash into the input's value?

Note: I've found several ways to do this when people are required to click a button, but nothing that explains how to do it automatically when the page loads.

ShadowAccount
  • 171
  • 2
  • 7
  • Does this answer your question? [Is there an onload event for input elements?](https://stackoverflow.com/questions/3708850/is-there-an-onload-event-for-input-elements) – Kinglish Jun 06 '21 at 20:19

3 Answers3

1

You'll need to assign this after the page loads, or at least the element

<input type="text" class="form-control" id="hash" value="">
<script>
  window.onload=function() {
     document.querySelector("#hash").value = window.location.hash
  }
</script>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

Just get the element and change its value using JavaScript. In this Snippet, I'm redirecting to a different hash, just for example purposes.

const input = document.querySelector("input.form-control");

// Redirect to different hash for example
window.location.hash = "abcdef";

input.value = window.location.hash;
<input type="text" class="form-control" />
0

You need JS script for that:

document.addEventListener('DOMContentLoaded', () => {
  const input = document.getElementById('location');
  input.value = window.location.host; // change .host to .hash
})
<input  id="location">
Alcadur
  • 681
  • 1
  • 8
  • 22