How to display a one-time element on a page via JS?

  • 0
    & lt; div id = "block" & gt; & lt; / div & gt;
    var block = document.getElementById('block');
    var elem = document.createElement('p');
    
    elem.textContent = 'one text';
    block.appendChild(elem);
    
    elem.textContent = 'two text';
    block.appendChild(elem);


    This is the code that outputs:
    two text
    And I need the output to be:
    one text
    two text
    That is, 2 paragraphs were created with different text but with the same variable name
    JavaScript Aaron Farley, Jan 4, 2020

  • 1 Answers
  • 0
    with different text but with the same variable name


    I don’t know why you needed it this way:

    var block = document.getElementById('block');
    var elem = document.createElement('p');

    elem.textContent = 'one text';
    block.appendChild(elem);

    var elem = document.createElement('p');

    elem.textContent = 'two text';
    block.appendChild(elem);
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!