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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Location
    MA, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using a variable in a path

    Hi -

    I have three graphics, and each has an associated link that - when clicked - will increase the width of the graphic. I'm trying to write a function that passes a variable (which of the three has been clicked) which would widen the appropriate bar. In a nutshell I have:

    <head>
    <script language="javascript">
    function change(a) {
    var mybar = 'document.bar'+a+'.width';
    mybar = mybar + 1;
    }
    </script>
    </head>
    <body>
    <img src="box.jpg" name="bar1" width="10"><a href="#" onclick="change('1');">change</a> <br>
    <img src="box.jpg" name="bar2" width="10"><a href="#" onclick="change('2');">change</a> <br>
    <img src="box.jpg" name="bar3" width="10"><a href="#" onclick="change('3');">change</a> <br>
    </body>

    The only way I was able to get this to work was to create an if..else if statement that checked for each number. Only thing is this could grow to many more than three items, so I wanted a function that checks everything then increases the width in the appropriate bar. New to javascript, and any help is much appreciated....thanks

    - cooter

  • #2
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Try:

    <script language="javascript">
    function change(a) {
    str='bar'+a;
    var mybar = 'document.images[str].width';
    mybar = mybar + 1;
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    function change(a) {
    document.images['bar'+a].width++;
    }

  • #4
    New to the CF scene
    Join Date
    Jan 2004
    Location
    MA, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks to both. I'm back on track...

    one question, did you use 'images' in the path because of the variable right after it? Is that something that should be done even if you know the image's name?? just wondering...

    - cooter

  • #5
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Images is the collection of images in the document object. If you just reference the name it will not work in all browsers, and in some browsers you may not be able to refrence all the attributes

    document.somename come from the Microsoft sloppy coding school. They encourage its use because the page break in most non-Microsoft browsers.

    Anytime you abberiated the DOM references you will have problems with browsers that follow the standards.
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&


  •  

    Posting Permissions

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