It is perfectly working. I want to get the current URL and set it inside textarea or input tag. Actually the value is set into a p
tag.
function urlf() {
document.getElementById("root").innerHTML = "The full URL of this page is: < br > " + window.location.href;
}
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<button id="my btn" type="button" onclick="urlf()">
get
</button>
<p id="root"> </p>
It is perfectly working. I want to get the current URL and set it inside textarea or input tag. Actually the value is set into a p
tag.
function urlf() {
document.getElementById("root").innerHTML = "The full URL of this page is: < br > " + window.location.href;
}
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<button id="my btn" type="button" onclick="urlf()">
get
</button>
<p id="root"> </p>
How can I do this?
Share Improve this question edited Feb 5, 2018 at 16:43 messerbill 5,6291 gold badge31 silver badges39 bronze badges asked Feb 5, 2018 at 15:38 KhanKhan 511 silver badge9 bronze badges 1-
Then create an
input
and assign what you want to itsvalue
property. – Federico klez Culloca Commented Feb 5, 2018 at 15:40
4 Answers
Reset to default 3Create an input
tag and set its value
function urlf() {
document.getElementById("root").value = "The full URL of this page is:" + window.location.href;
}
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<button id="my btn" type="button" onclick="urlf()">
get
</button>
<input type="text" style="width:100%;" id="root">
you set the innerHTML
of the element with the id root
. According to your code this is:
<p id="root"> </p>
so if you want to load the content into an input field you need to change the following:
document.getElementById("root").value = "The full URL of this page is:" + window.location.href;
<input id="root" type="text" />
greetings
<input id="root" type="text">
function urlf() {
document.getElementById("root").value = window.location.href;
}
You just want the current URL in a input? Like below:
function urlf() {
document.getElementById("root").value = "The full URL of this page is:" + window.location.href; }
<div>
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<button id="my btn" type="button" onclick="urlf()">
get
</button>
<input style="width:500px;" type="text" id="root"/>
</div>