Click to See Complete Forum and Search --> : Simple Simple Stuff
DJRobThaMan
07-23-2003, 01:52 PM
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
var me, i = 1;
function callit(){
me = eval("ment"+i);
document.getElementById(me).style.width=100+"px";
}
[J]ona
David Harrison
07-23-2003, 01:58 PM
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.
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
DJRobThaMan
07-23-2003, 02:11 PM
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??????
David Harrison
07-23-2003, 02:18 PM
How's this for a change colour background, and no js involved:
<!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
My code was escaped. The second internal strings should be \' not ' (around the backgroundColor='whatever=\' not backgroundColor='whatever=')
[J]ona
DJRobThaMan
07-23-2003, 02:24 PM
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.
Here's a text file with the real code, because the forum keeps escaping my code.
[J]ona
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
DJRobThaMan
07-23-2003, 02:58 PM
Thanks Jona. That last one really helped. Time to try messing with arrays now. Yay...
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
DJRobThaMan
07-23-2003, 03:12 PM
lol....
I stand corrected
Originally posted by DJRobThaMan
I stand corrected
I only meant to help. ;)
[J]ona