I have the 1 button and some text in my HTML like the following:
function get_content(){
// I don't know how to do in here!!!
}
<input type="button" onclick="get_content()" value="Get Content"/>
<p id='txt'>
<span class="A">I am</span>
<span class="B">working in </span>
<span class="C">ABC company.</span>
</p>
When the user clicks the button, the content in the <p id='txt'>
will become the follow expected result:
<p id='txt'>
// All the HTML element within the <p> will be disappear
I am working in ABC company.
</p>
Can anyone help me how to write the JavaScript function?
Thank you.
You can use this:
var element = document.getElementById('txt');
var text = element.innerText || element.textContent;
element.innerHTML = text;
Depending on what you need, you can use either element.innerText
or element.textContent
. They differ in many ways. innerText
tries to approximate what would happen if you would select what you see (rendered html) and copy it to the clipboard, while textContent
sort of just strips the html tags and gives you what's left.
innerText
also has compatability with old IE browsers (came from there).