Forum Moderators: open

Message Too Old, No Replies

help with syntax

         

JimHo

2:37 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



I need a little help with syntax in a function. I am trying to use a variable in the getelementbyid method as follows:

function update(series)
{
var icon =eval(series +"icon");

document.getElementById( icon ).style.MozOpacity=1;
document.getElementById( icon ).filters.alpha.opacity=100;
}

I am trying to define the opaque property of Image1 at runtime based on mouseover events of Image2.

Thanks Jim

Fotiman

3:33 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove the eval portion. Assuming series is a string or a number, you can just concatinate them without using eval.

Also, you should probably add a check to make sure the element was found before you try to modify it's properties.


function update(series)
{
var icon = document.getElementById( series + "icon" );
if(!icon ) return;
icon.style.MozOpacity=1;
icon.filters.alpha.opacity=100;
}

JimHo

4:50 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



Thank You. I appreciate your help!

JimHo

5:51 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



I must be doing something wrong here

I set up the following to test:

function update(series)
{
var icon = document.getElementById(series + "icon");
icon.filters.alpha.opacity=100;
}

I then checked it in Firefox's Javascript Console, and it choked on the "icon.filters.alpha.opacity=100;" line with an error code - icon has no properties

Any ideas?

Fotiman

6:26 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




function update(series)
{
var icon = document.getElementById(series + "icon");
icon.filters.alpha.opacity=100;
}

I then checked it in Firefox's Javascript Console, and it choked on the "icon.filters.alpha.opacity=100;" line with an error code - icon has no properties

1. That's why you should have the check:
if(!icon ) return;
In your case, though, you might use this to debug by doing this:


if(!icon )
{
alert('unable to find icon with id: ' + series + 'icon');
return;
}

If it gives you the alert, then you must not be searching for the right thing.

2. I could be wrong, but I think filters are an IE only thing. So when you try and modify the filter property of an object, Firefox (and any other non-IE browser for that matter) will probably barf because the property doesn't exist. Maybe you could do something like this to get around it:
if( icon.filters )
{
if( icon.filters.alpha )
{
icon.filters.alpha.opacity=100;
}
}

JimHo

7:42 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



Got it thanks!