Skip to main content
Jeffery.Wright
Inspiring
October 9, 2023
Answered

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

  • October 9, 2023
  • 2 replies
  • 774 views

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 has been closed for replies.
Correct answer JoãoCésar17023019

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

 

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
October 9, 2023

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

 

Jeffery.Wright
Inspiring
October 10, 2023

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!

 

 

 

kglad
Community Expert
Community Expert
October 10, 2023

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).

kglad
Community Expert
Community Expert
October 9, 2023

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

Jeffery.Wright
Inspiring
October 9, 2023

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. 

kglad
Community Expert
Community Expert
October 9, 2023

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.