Closed Thread Icon

Topic awaiting preservation: sending style attributes to function (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8847" title="Pages that link to Topic awaiting preservation: sending style attributes to function (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: sending style attributes to function <span class="small">(Page 1 of 1)</span>\

 
smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 09-04-2003 17:37

hi, please can you help,

why doesn't this work:

var TYPE = 'backgroundColor';

getElementByID('myDiv').style.TYPE = '#FFFFFF';

Ok I realise that this code isn't that useful, but what I'm trying to figure out is how to pass a 'style type' dynamically. Do I need to set the variable 'TYPE' as a 'new Object' or something?

Please help if you can.

Jon

MajorFracas
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2003

posted posted 09-04-2003 18:52

Try this instead:

code:
getElementById('myDiv').style.setProperty(TYPE,'#FFFFFF');



bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-04-2003 19:14

He's got a better method but the root of your problem is that accessing elements like that doesn't resolve variables.

This is where an eval statement would be needed.


eval("getElementByID('myDiv').style."+TYPE+" = '#FFFFFF'");




.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-04-2003 19:57

No no no, eval function calls are *not* needed to turn strings into properties! JavaScript has a specific method for handling this:

var TYPE = 'backgroundColor';

getElementByID('myDiv').style[TYPE] = '#FFFFFF';

If you have the property as a string, use the [] operator instead of the dot (.) operator. It's that simple!

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 09-04-2003 21:35

Ah, I see, so just like in Actionscript then slime, cool. I stupidly never tried that method although I had thought about it - I automatically assumed it would end up calling some non-existent associative array, so now you see why I'm not cut out to be a coder.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-05-2003 00:57

It *does* sort of index an array. Or, more accurately, arrays are the same as objects, and when you do myArray[0], that's *almost* exactly the same as myArray.0 (the only difference is that the latter isn't valid syntax, since properties have to start with letters). So, internally, that's pretty much what's going on.

By the way, Actionscript is based off the same language as JavaScript (it's called ECMAScript), so pretty much anything that's valid in one is valid in the other.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 09-05-2003 10:24

I know about the ECMAScript standard - netscape made JavaScript, then some people came along and standardised the whole thing into ECMAScript then MM made ActionScript from the ECMAScript standard, which is now also followed by JavaScript - or somethign along those lines. ActionScript 2.0 is even more standards compliant apparently.

Out of interest tho, is ECMAScript an actual usuable language in it's own right, or just a standardised format for other scripts to be based on?

[This message has been edited by smonkey (edited 09-05-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-06-2003 00:35

Technically, I think it's a language in it's own right. However, it's useless in the sense that it doesn't contain any methods of *doing* anything. Only when a language (JavaScript, ActionScript) is based on it, and given predefined objects to work with (like the document object) does it have any power. So, in that sense, it's sort of a "base" language.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-07-2003 05:23

wow... that kinda sucks.... 'an emasculated language useful only as a skeleton set for new languages'

... kinda makes a language want to commit Hari Kari..

you think one could base a scripting language off of it, like, say, an AI for a game or something?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu