I cannot find an answer to this question...
The problem. Javascript copying text from DIV to input (textbox), fails to copy &
as &
, and copies it as &
. See code plus example.
I have the following code (on different pages, just to simplify I put all here):
INPUT where I want the text copied.
<input name="artist" id="txtArtist" type="text" placeholder="Introduce the artist" />
DIV where the text is copied from. This data is populated from a SQL database.
<div id="art0"><div onclick="copyText(0)">Mumford & Sons</div></div>'
The function that fails...
function copyText(rowID){
var content = document.getElementById('art'+rowID).innerHTML.replace(/<\/?[^>]+(>|$)/g, "
");
document.getElementById("txtArtist").value = document.getElementById('art'+rowID).innerHTML;
document.getElementById("txtArtist").value = content;
}
EXPECTED result inside textbox: Mumford & Sons
ACTUAL result inside textbox: Mumford & Sons
Any help please?
Thank you all!!
Use .textContent
instead of .innerHTML
:
var content = document.getElementById('art'+rowID).textContent;
Note that .textContent
is not supported by IE 8 and lower, so if you are so unfortunate, test for and use .innerText
instead.
var prop = 'textContent' in document.body ? 'textContent' : 'innerText',
content = document.getElementById('art'+rowID)[prop];