最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - set value of html input or textarea - Stack Overflow

programmeradmin2浏览0评论

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 its value property. – Federico klez Culloca Commented Feb 5, 2018 at 15:40
Add a ment  | 

4 Answers 4

Reset to default 3

Create 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>

发布评论

评论列表(0)

  1. 暂无评论