www.webdeveloper.com
Results 1 to 15 of 15

Thread: Simple Simple Stuff

  1. #1
    Join Date
    Jul 2003
    Posts
    259

    Simple Simple Stuff

    var me;
    var i=1;
    function callit(){
    me = eval('"ment" + i');
    document.me.style.width = 100;
    }

    Does anybody know why this doesn't work?



    Thanks


    Lata

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413

    var me, i = 1;

    function callit(){
    me = eval("ment"+i);
    document.getElementById(me).style.width=100+"px";
    }


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    I don't know how eval works really, I've never used it myself, but couldn't you just do this:

    document.eval('"ment" + i').style.width = 100;

    I THINK the problem is that it's getting confused as to which variable to use, the variable outside the function, or the one inside.

    What you could also do is remove the:

    var me;

    because if you don't use the var keyword in your functions to declare variables they are created as global variables.
    Every fight is a food fight when you’re a cannibal.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    The code will actually work fine in IE (I used getElementById() for Netscape compatibility), if you add the "px" string to the end:


    document.me.style.width=100+"px";


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Jul 2003
    Posts
    259
    Okay, hopefully I actually get to post this time. I don't know why but I'm having a really big problem with this forum.
    Anyway, I suppose I could put it all in one but I did it like I did because it wouldn't work when I did it that way before and so I put that by itself and used the document.writln function to make sure it actually did what I wanted. So I know if I go document.writeln(me) it writes "ment1" which means that the variable me is defined as "ment1".

    And that is the name for an image (right now that's just a test, I don't even have and image, just an area that I'm looking at to see if the coding works. When the code works then I'm going to apply to the id of divs so that I can do some cool stuff with them) so the image background should change from blue (that's what I have it at now) to red, but an error comes up saying that document.me is null or not an object.


    Anybody have any ideas??????

  6. #6
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    How's this for a change colour background, and no js involved:
    Attached Files Attached Files
    Every fight is a food fight when you’re a cannibal.

  7. #7
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en-US">
    <head><title>My Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Style-Type" content="text/css">
    <meta name="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    <!--
    function edit(obj, specs){
    eval("document.getElementById(obj).style."+specs);
    }
    //-->
    </script>
    <style type="text/css">
    <!--
    #myDiv { background-color:blue; }
    -->
    </style>
    </head>
    <body>
    <div id="myDiv" onMouseOver="edit(this, 'backgroundColor=\'red\'');">
    Lorem ipsum solor dit amet
    </div>
    <form action="">
    <input type="button" value="Turn blue" onClick="edit('myDiv', 'backgroundColor=\'blue\'');"><br>
    <input type="button" value="Turn red" onClick="edit('myDiv', 'backgroundColor=\'red\'');">
    </form>
    </body>
    </html>


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  8. #8
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    My code was escaped. The second internal strings should be \' not ' (around the backgroundColor='whatever=\' not backgroundColor='whatever=')

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  9. #9
    Join Date
    Jul 2003
    Posts
    259
    Well, it's not really the colour I mean to change when I actually get the code to work. I just want to be able to call an element on the page like that so I can do anything with it, move it change the colour, size, whatever.


    Thanks tho.

  10. #10
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Here's a text file with the real code, because the forum keeps escaping my code.

    [J]ona
    Attached Files Attached Files
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  11. #11
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by DJRobThaMan
    Well, it's not really the colour I mean to change when I actually get the code to work. I just want to be able to call an element on the page like that so I can do anything with it, move it change the colour, size, whatever.
    This code should help you get down that pathway.

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  12. #12
    Join Date
    Jul 2003
    Posts
    259
    Thanks Jona. That last one really helped. Time to try messing with arrays now. Yay...

  13. #13
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by DJRobThaMan
    document.writeln function
    Just so you know, it's actually a method (which is like a function but it's attached to a specific object or type of object). In this case, it's a built-in method of the document object.

    Tell me if you need any help on those arrays.


    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  14. #14
    Join Date
    Jul 2003
    Posts
    259
    lol....


    I stand corrected

  15. #15
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by DJRobThaMan
    I stand corrected
    I only meant to help.

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles