getElementByID in javascript [object HTMLSpanElement] - Stack Overflow

I have the following problem... i want to replace a text of a div with an other text of a span.So I tri

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 the export_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
Add a ment  | 

1 Answer 1

Reset to default 4

Justt 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

相关推荐

  • getElementByID in javascript [object HTMLSpanElement] - Stack Overflow

    I have the following problem... i want to replace a text of a div with an other text of a span.So I tri

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信