I have the following problem... i want to replace a text of a div with an other text of a span.
So I tried this:
<main>
<span id="export_text">This text should be exported</span><br>
<div id="import_text">Here the new text should be after pressing the button</div><hr>
<a onclick="run()"><button>run</button></a>
</main>
<script type="text/javascript">
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text');
}
</script>
now if i press the button the text in the div gets replaced with [object HTMLSpanElement] instead of the text from the span. What did i do wrong here?
Thank you!
I have the following problem... i want to replace a text of a div with an other text of a span.
So I tried this:
<main>
<span id="export_text">This text should be exported</span><br>
<div id="import_text">Here the new text should be after pressing the button</div><hr>
<a onclick="run()"><button>run</button></a>
</main>
<script type="text/javascript">
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text');
}
</script>
now if i press the button the text in the div gets replaced with [object HTMLSpanElement] instead of the text from the span. What did i do wrong here?
Thank you!
Share Improve this question asked Jul 3, 2015 at 10:06 look002look002 131 silver badge2 bronze badges 3-
You forgot to put
innerHTML
onto the end of theexport_text
element – CodingIntrigue Commented Jul 3, 2015 at 10:08 -
Change to
document.getElementById('export_text').innerHTML
, don't just assign that element. – fuyushimoya Commented Jul 3, 2015 at 10:08 - im still getting the same – look002 Commented Jul 3, 2015 at 10:10
1 Answer
Reset to default 4Justt add .innerHTML
at the end of your export text selector:
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text').innerHTML;
}
I also strongly suggest you to move your event in JS:
document.getElementById("myBtn").addEventListener("click", run);
Updated HTML:
<main> <span id="export_text">This text should be exported</span>
<br>
<div id="import_text">Here the new text should be after pressing the button</div>
<hr> <a><button id="myBtn">run</button></a>
</main>
JSFiddle: http://jsfiddle/ghorg12110/n4cwem28/1/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745267057a4619521.html
评论列表(0条)