开发者

pass a html text in an input field using HTML DOM by innerHTML

开发者 https://www.devze.com 2023-03-21 07:31 出处:网络
I have a problem in geting the html content from a div am pass it into an input field I\'m using the following code:

I have a problem in geting the html content from a div am pass it into an input field I'm using the following code:

<html>
<body>
<div id="void"> 
<div id="main"><strong>Hello</strong> my friend</div>
</div>
<br>
<input type="text" id="resul" >
<br>
<script type="text/javascript">
x=document.getElementById("void").getElementsByTagName("div");
document.write("Text of first paragraph: " + x[0].innerHTML);
document.getElementById("resul").value=x[0].innerHTML;
</script>
</body>
</html>

If you will try it, you will see what I mean. By using the command document.write() I get the value that I need from de div tag : "Hello my friend" but when 开发者_开发问答I want to pass this value "Hello my friend" into an input field I get something like this "Hello my friend". How can I pass into the input field only the text "Hello friend" without tag.

Any help would be much appreciated! Thanks!


How about this:

<script type="text/javascript">
var x = document.getElementById("main");
document.write("Text of first paragraph: " + x.innerHTML);
document.getElementById("resul").value= x.innerHTML;
</script>

to pass the content without the tag you can use

x.textContent || x.innerText;

so:

var x = document.getElementById("main");
var text = x.textContent || x.innerText;
    document.write("Text of first paragraph: " + text);
    document.getElementById("resul").value = text;
0

精彩评论

暂无评论...
验证码 换一张
取 消