textContent only works on initial page load or within an event callback function

textContent only works on initial page load or within an event callback function
0.0 0

#1

For example if I use display.textContent = “Hello”; in a function later in my code it won’t update the text. Does anyone know why this is the case? I want it to update in my function.


#2

Without context is a pretty vague, but you can always be able to get an element and set its contexts as:

document.getElementById('example').textContent = "This is some text";

With a basic event listener

document.getElementById(whatever).onclick = function() {
 document.getElementById(theTextIWantToChange).textContent = "This is some text";
};

Hope it gives you a good start :slight_smile:


#3

The set text is within a function which is called by a click event.

click event -> call to function -> inside function textContent statement doesn’t work.

This is a personal project I’m working on. JavaScript is such garbage compared to Java, C++ and Python for me so far. Not enjoying this language.


#4

post your code before calling JS garbage.


#5

I solved this issue on my own. Thanks.


#6

What was your final solution to remedy your issue?


#7

I created my own callback function to update the value I needed. Previously when the textContent was in another function that was called by an event, it wouldn’t change my textContent. It just ran all the other code within that function’s block. JavaScript presented no errors and simply didn’t run my statement which was quite frustrating.