0

/* main.js */
myP1 = document.createElement('p');
myP2 = document.createElement('p');

firstDiv = document.querySelector('div');
firstDiv.appendChild(myP1);
firstDiv.appendChild(myP1);

myP1.textContent = 'P1 here';
p2TextContent = myP2.textContent;
p2TextContent = 'P2 here';
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <div>
    </div>
  </body>
  <script src = 'main.js'></script>
</html>

When myP2.textContent is assigned to variable p2TextContent, p2TextContent should be bound to myP2.textContent, and if modified p2TextContent, myP2.textContent should be modified at the same time. But why myP2.textContent has not been modified?

Teemu
  • 22,918
  • 7
  • 53
  • 106
Zou Xin
  • 113
  • 1
  • 7
  • By dynamic they mean user interactive or results driven. You haven't assigned any `.textContent` to `myP2` when your first attempt to get it's value. – StackSlave Oct 10 '17 at 05:00
  • `myP2.textContent` returns plain text, i.e. a primitive, and a line later `p2Content` is just reassigned with a new value. Setting `p2TextContent = myP2.textContent;` doesn't create a magical link between the variable and the HTML element. – Teemu Oct 10 '17 at 05:03
  • Why would you think it is supposed to work like that? If you want binding, use a template or other system which provides binding. –  Oct 10 '17 at 05:05
  • everything in js is passed by value, though with objects that value is a pointer to the object... – dandavis Oct 10 '17 at 05:21

0 Answers0