-3
//I am trying change text after click on it in web page

text = document.querySelector(".text");
text.addEventListener("click", changeText=()=> {
 
    text = document.textContent = "NewText";

});

But it is not working. Why? I have try to use innerHTML? but it isnot working yet...

Sascha
  • 4,576
  • 3
  • 13
  • 34

2 Answers2

1

You have to set text.textContent instead of document.textContent to the new value.

text = document.querySelector(".text");
text.addEventListener("click", function() {
     text.textContent = "NewText";
});
<span class="text">Text</span>
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

You are reassigning text to document.textContent, which is not what you want.

// Get a reference to the element you'll want to change
text = document.querySelector(".text");

// Set up an event handler on that element
text.addEventListener("click", changeText=()=> {
    // Change the .textContent of that element:
    text.textContent = "NewText";

    // Within the DOM element event handler, you can also 
    // refer to the element that the event was triggered on
    // with the "this" keyword, so you could also do this:
    //this.textContent = "NewText";
});
<span class="text">Original Text</span>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71