Skip to main content
Jeffery.Wright
Inspiring
Oct 9, 2023 3:58 PM UTC
Answered

HTML5 Canvas JS: Store Path to Dynamic text as Variable?

Writing this out every time for the numerous click functions I have seems a bit redundant:

 

exportRoot.TXT_add.text = "Some Value";
exportRoot.TXT_ext.text = "Another Value";

 

This would be tidier:

 

ADDR = "Some Value";
EXT = "Another Value";

 

There must be some way to do this, but I can't find anything on the topic.
This is my attempt:

var ADDR = exportRoot.TXT_add.text;
var EXT = exportRoot.TXT_ext.text;

clear_INFO=function(){
ADDR = " ";
EXT = " ";
	}

clear_INFO();

 

That should clear the content of the Dynamic text objects, but it does nothing. And it also does not produce a console error, shouldn't it?

 

Is this sort of thing simply not possible? Looking for clues, thanks. 

 

 

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Oct 9, 2023 8:58 PM UTC

Hi.

 

The .text is a property of a text field and not the text field itself. So when you store a reference to it you only storing a reference to a primitve value.

 

What you can do is to store references to the text fields themselves like this:

var addTF = exportRoot.TXT_add;
var extTF = exportRoot.TXT_ext;

 

Them set their texts properties like this:

addTF.text = "";
extTF.text = "";

 

Regards,

JC

 

8 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Oct 9, 2023 8:58 PM UTC

Hi.

 

The .text is a property of a text field and not the text field itself. So when you store a reference to it you only storing a reference to a primitve value.

 

What you can do is to store references to the text fields themselves like this:

var addTF = exportRoot.TXT_add;
var extTF = exportRoot.TXT_ext;

 

Them set their texts properties like this:

addTF.text = "";
extTF.text = "";

 

Regards,

JC

 

Translate
Jeffery.Wright
Inspiring
Oct 10, 2023 8:09 PM UTC

Thanks man, at least I was close... I wonder why the variable can't store .text  as part of the complete path to the object? 

To me, intuitively, I would think the variable 

ADDR = exportRoot.TXT_add.text;


would mean, that ADDR would be the same value as exportRoot.TXT_add.text and equating it with a string, would be exactly like 

exportRoot.TXT_add.text = "Some Value";

 

Variables then cannot refer to the property of the object at the end of its path? That sure would be useful. 

Thanks again!

 

 

 

Translate
kglad
Community Expert
Community Expert
Oct 10, 2023 8:14 PM UTC

the text property to a textfield isn't different from the x property of an object.  ie, if you store the x property, you're storing nothing about the object.

 

if you want to store a reference to the object, do exactly that and do not store a property of the object (unless that's you want to access, the property).

Translate
kglad
Community Expert
Community Expert
Oct 9, 2023 4:06 PM UTC

it's not clear what you're trying to do unless you have more than 2 textfields.  do you?

Translate
Jeffery.Wright
Inspiring
Oct 9, 2023 8:00 PM UTC

Any number of text fields, is there a way to store the path to them in a variable?

There are two in my example, thanks. 

Translate
kglad
Community Expert
Community Expert
Oct 9, 2023 8:15 PM UTC

you can use bracket notation to coerce strings to objects.

 

var s = "add"

exportRoot[TXT_ + s].text = whatever;

 

this is typically used with arrays (or more perceptive object names) and for-loops.

Translate