JavaScript Using getElementById to duplicate content in page document.getelementbyid duplicate id

I don't know javascript and I have been searching every where for this answer. I would like to duplicate content in my page. The html and content comes directly from a broker.

The result wanted is :

Click the button to change the text in this paragraph.
Click the button to change the text in this paragraph.

My HTML is:

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the text in this paragraph.</p>
<script language="javascript" type="text/javascript">
var elem = document.getElementById('demo').setAttribute('id', 'nextstep');
document.write(elem);
</script>
</body>
</html>

What I am getting is:

Click the button to change the text in this paragraph.
undefined

Can somebody help point me in the right direction? Thanks in advance!

Answer:1

You need to grab the innerHTML and set it:

var elem = document.getElementById('demo').innerHTML;
document.write(elem);

Beware though, document.write is going to overwrite everything..

Answer:2

If u need to get this

<p id="demo">Click the button to change the text in this paragraph.</p>
<p id="nextstep">Click the button to change the text in this paragraph.</p>

try

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the text in this paragraph.</p>
<script language="javascript" type="text/javascript">
    var elem = document.getElementById('demo');
    var newElem = document.createElement('div');
    newElem.innerHTML = "<p id='nextstep'>" + elem.innerHTML + "</p>";
    document.write(newElem.innerHTML);
</script>
</body>
</html>
Answer:3

I have a polymer input field like this: <paper-input type="text" class="user-value" data-id="user-value" value="{{typingAns}}" focus maxlength="10" /> is it possible to block the backspace ...

I have a polymer input field like this: <paper-input type="text" class="user-value" data-id="user-value" value="{{typingAns}}" focus maxlength="10" /> is it possible to block the backspace ...

I am working on a page where I need to have some similar trees (kind of) and leaves on them and have lots of them, as in like spanning across page width at page bottom. I was thinking of doing it 2 ...

I am working on a page where I need to have some similar trees (kind of) and leaves on them and have lots of them, as in like spanning across page width at page bottom. I was thinking of doing it 2 ...

  1. html create large text box
  2. html create large text

I have a survey web app at work to collect data from customers. I'm trying to convert a dozen different functions (one for each option) into one simple function. The "votes" are stored as ...

I have a survey web app at work to collect data from customers. I'm trying to convert a dozen different functions (one for each option) into one simple function. The "votes" are stored as ...

  1. passing value from html to python
  2. passing value from child to parent angular
  3. passing value from one input to another
  4. passing value from groovy script to request
  5. passing value from python flask to html
  6. passing value from child to parent react
  7. passing value from html to python django
  8. passing value from javascript to php
  9. passing value from controller to view
  10. passing value from adapter to fragment
  11. passing value from activity to fragment
  12. passing value from controller to view in mvc
  13. passing value from javascript to html
  14. passing value from flask to html
  15. passing value from child to parent vue
  16. passing value from javascript to jsp
  17. passing value from servlet to jsp
  18. passing value from js to html
  19. passing value from form2 to form1 c#
  20. passing value from one html to another

I have an unordered list called test <ul id='test'></ul> it is dynamically populated with data via ajax. Each item 'li' is a div containing 'p' paragraphs. Each paragraph contains some ...

I have an unordered list called test <ul id='test'></ul> it is dynamically populated with data via ajax. Each item 'li' is a div containing 'p' paragraphs. Each paragraph contains some ...