Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    newbie. Variable names contained in variables

    Can an attribute be made variable? For example, could
    Code:
    document.getElementById('body').style.backgroundColor = '#ffffff'
    become
    Code:
    document.getElementById('body').style.--something or other--  = --some expression--
    where --something or other -- might be a variable containing or a function returning the name of the attribute to be set?

  • #2
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    document.getElementById('body').style[attribute]=watever;

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by trib4lmaniac
    document.getElementById('body').style[attribute]=watever;
    OK thank you, but I think I chose a poor example. Isn't it that the particular feature I chose as an example -- style -- is an array, and can be done that way?

    I'm looking at a general way to hold variables within variables; I'm guessing that there is no such feature. Well I can't find one anyway.

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not entirely sure what you mean then! That method can be used very well, in many different ways.
    Code:
    document.body.style.background='#369';
    document.body.style['background']='#369';
    document.body['style']['background']='#369';
    document['body']['style']['background']='#369';

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    trib4lmaniac is correct. Consider this demo:
    Code:
    function setStyle(id, attName, attValue){
      document.getElementById(id).style[attName]=attValue;
    }
    ...
    <div id="myDiv">Blah Blah</div>
    <input type="button" value="Change Background" onclick="setStyle('myDiv','backgroundColor','yellow');" />
    <input type="button" value="Change Color" onclick="setStyle('myDiv','color','red');" />
    More info on bracket notation here.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    First, what do you want exactly? To get a styled attribute or to set a styled attribute? Or to get a class? To change a class?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, I think I'm perhaps asking the wrong question and that the feature you outline is in fact what I want. Thank you.

    What I was asking was a subset of 'can you dynamically build code (into a variable) and then execute it?'

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well - for more general code/strings - there's always eval(), but I don't think many people here would recommend that!

  • #9
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    First, what do you want exactly? To get a styled attribute or to set a styled attribute? Or to get a class? To change a class?
    1) I must choose my examples better .
    2) In the particular case I am trying, to set attributes. I think the [] notation is what I want
    Quote Originally Posted by trib4lmaniac
    Well - for more general code/strings - there's always eval(), but I don't think many people here would recommend that!
    Because of the inefficiencies? And/or because it's hard to understand?

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Roy Gardiner
    Because of the inefficiencies? And/or because it's hard to understand?
    http://www.codingforums.com/showthread.php?t=20143
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •