...

View Full Version : converting strings to variables



Tails
01-17-2003, 09:30 PM
Is it possible to convert a string to a variable? Well, here's one example with tables. In the function, X is a number passed on by an href or onClick event. You can't assign an ID as a numer though. So I tried giving each table an ID like t0, t1, t2, etc and
just combine numbers from the event passed on to a t to form the ID and work with one function instead of one per table.

function tab(X)
{
A="t"+X
A.style.backgroundColor="blue"
}

But now A is a string. I can't pass t0 or t1 as an event becaues it's
undefined.
onMouseOver="tab(t0)"
But if I put them in quotes in it
onMouseOver="tab('t0')"
it is also treated like a string and won't be recognized as anything when trying to get that to work as an ID. What can I do to fix this?

mordred
01-17-2003, 09:35 PM
Well, you use the function that's designed to access the HTML element as defined by it's Id value:

document.getElementById("t"+ X).style.backgroundColor = 'Fnord';

Roy Sinclair
01-17-2003, 09:37 PM
function tab(X)
{
A=document.getElementById("t"+X)
A.style.backgroundColor="blue"
}



This should work or even shorter:



function tab(X)
{
document.getElementById("t"+X).style.backgroundColor="blue"
}

Tails
01-18-2003, 07:19 PM
Thanks, that is really useful. I try to keep my scripts as compact as possible, but sometimes after a week of not working on something, even I forget my logic. I should really start commenting my lines.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum