0

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!

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.