Stuck for several days

J

Javier Troconis

Hello, first of all it's convenient to say that I'm not an expert, neither
nothing similar, just in case. I have been trying to execute this little
piece of code with no results, does anyone there can detect where the bug
is:

var labelsArray = new Array(6);

----------------------------------------------------------------------------
----------------
function objCreation()
{
for (i=0; i < labelsArray.length; i++)
{
var labelObj = {labelID:
"document.getElementById('Label"+i+"').filters.alpha.opacity"};
labelsArray = labelObj;
}
}

function over(no)
{
eval(labelsArray[no].labelID = 20);
}

----------------------------------------------------------------------------
---------------

Basically, the function objCreation creates 6 objects with only one
attribute (labelID) each one and stores it into an Array called labelArray
Then the function over(no) is loaded when the MouseOver event takes place.
The problem is that since
labelsArray[no].labelID is equal to the string
document.getElementById('Label0').filters.alpha.opacity for example, it is
not recognized as a functional statement, as a result it doesn't do anything
if I assign the value 20 in this case.

The website is at : http://new.vanara.com/ , if someone wants to see it.

Thanks in advance,
Javier.
 
R

Richard Cornford

var labelsArray = new Array(6);
function objCreation()
{
for (i=0; i < labelsArray.length; i++)
{
var labelObj = {labelID:
"document.getElementById('Label"+i+"').filters.alpha.opacity"};
labelsArray = labelObj;
}
}

function over(no)
{
eval(labelsArray[no].labelID = 20);


If you are not an experienced javascript programmer and you are using
the - eval - function you can be certain that you are doing something
fundamentally wrong.

<snip>

You have omitted one very important detail and that is why. Why jump
through such hoops and use such a convoluted method? When on the face of
it your entire posted code could be replaced with:-

function over(no){
document.getElementById('Label"+no).filters.alpha.opacity = 20;
}

- and achieve your apparent goal in the browsers where it will work.
Or:-

function over(no){
var obj;
if((document.getElementById)&&
(obj = document.getElementById('Label"+no))&&
(obj.filters)&&
(obj.filters.alpha)){
obj.filters.alpha.opacity = 20;
}
}

- Which achieves the same but won't error on the majority of web
browsers in the way the previous version and your original code was
destined to.

Richard.
 
R

Richard Cornford

function over(no){
document.getElementById('Label"+no).filters.alpha.opacity = 20;
^ ^
document.getElementById("Label"+no).filters.alpha.opacity = 20;
<snip>

Except in both cases it will work better with the same type of quote
marks on each side of the string literal.

Richard.
 
E

Erwin Moller

Richard said:
^ ^
document.getElementById("Label"+no).filters.alpha.opacity = 20;
<snip>

Except in both cases it will work better with the same type of quote
marks on each side of the string literal.

Richard.

True, that often helps a lot. :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top