Click to See Complete Forum and Search --> : eval() variable?


michelle
09-29-2003, 02:33 PM
I want to change the "text"-property of "example1" using this code, but I have no idea of how I can do this.

I tried this, but it obviously didn't work.
eval(propName+'.text') = newText;

Any thoughts?
// Michelle

<CODE>

var example1 = new addProp('example1');
var example2 = new addProp('example2');

function addProp(text)
{
this.text = text;
this.font = "verdana";
}

function changeProperty(propName, newText) {
//eval(propName+'.text') = newText;
propName.text = newText;
}

<a href="javascript:changeProperty('example1', 'This is the new text')">change text</a>
<br>
<a href="javascript:alert(example1.text);">check value</a>

</CODE>

Jona
09-29-2003, 02:53 PM
newText = eval(propName+".text");


[J]ona

Charles
09-29-2003, 03:01 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<script type="text/javascript">
<!--
Array.prototype.next = function () {if (this.n == undefined || ++this.n == this.length) this.n = 0; return this[this.n]}

giantSays = ['Fee', 'Fie', 'Foe', 'Fum'];
// -->
</script>

<p onmouseover="this.firstChild.data = giantSays.next()">Giant Says</p>

michelle
09-29-2003, 06:06 PM
Originally posted by Jona
newText = eval(propName+".text");


Well this doesn't solve the problem, the process I am looking for is this:

1) I click this the first time and it alerts "example1"
<a href="javascript:alert(example1.text);">check value</a>

2) I click this, which changes "example1.text" to "this is teh new text".
<a href="javascript:changeProperty('example1', 'This is the new text')">change text</a>

3) I click this the second time and it alerts "This is the new text"
<a href="javascript:alert(example1.text);">check value</a>

This does not change the value of example1.text:
newText = eval(propName+".text");

Any thoughts?
// Michelle

Jona
09-29-2003, 06:18 PM
Are you sure Charles didn't answer your question? :p

[J]ona

Charles
09-29-2003, 06:22 PM
I'm not at all clear what it is that you are trying to accomplish but you should almost never use "eval()".

<script type="text/javascript">
<!--
example1 = new Object;
example1.test = 'example1';
window['example1'].test = 'new text';
alert (example1.test);
// -->
</script>