Solved

please see what is wrong with my JS code

Posted on 2012-03-22
8
211 Views
Last Modified: 2012-03-23
Hi,

Practicing a little JS..

Please see what is wrong with my JS code below:
It is meant to move the image across the screen.

<html>
<head><title>Our Universe</title>
<script type="text/javascript">

var i=0;
var timeout;
var elementStyle = document.images("image1").style;
elementStyle.position = "relative";
elementStyle.left = "5px";

function driveCar() {

      while (i<100) {
            elementStyle.left = i+"px";
            i++;
            timeout = setTimeout("driveCar()",1500);
      
}
}


</script>
<body onload="driveCar();">

<img src="images.jpg" name="image1" id="image1" />


</body>
</html>
0
Comment
Question by:jecommera
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 400 total points
Comment Utility
For one thing, you will not be able to access the image element from the head section until it has been rendered to the page. You could move your script block to somewhere below the image tag on the page.
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 100 total points
Comment Utility
how about this:

<script type="text/javascript">

var i=0;
var timeout;
var elementStyle = null;

function driveCar() {

      if (!elementStyle)
      {
            elementStyle = document.images("image1").style;
            elementStyle.position = "relative";
            elementStyle.left = "5px";
      }

      if (i<1000) {
            elementStyle.left = i+"px";
            i++;
            timeout = setTimeout("driveCar();",100);
      }
}

</script>

Open in new window

0
 

Author Comment

by:jecommera
Comment Utility
Thanks however I get error

document.images is not a function
elementStyle = document.images("image1").style;
0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 400 total points
Comment Utility
Well, if we are doing complete re-writes then how about this:
<body onload="driveCar_init();">

<img src="images.jpg" name="image1" id="image1" />

<script type="text/javascript">
var i=0;
var timeout;
var elementStyle = document.getElementById("image1").style;
elementStyle.position = "relative";
elementStyle.left = "5px";

function driveCar() {
    elementStyle.left = i+"px";
    i++; 
}
function driveCar_init(){
    timeout = setInterval("driveCar()",500);
}
</script>
</body>

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 400 total points
Comment Utility
document.images is a collection of all images on the page. Since you only have one, you could do this: document.images[0].style. Generally though it is more precise to select the element by id: document.getElementById("image1").style
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
wow, @tommyBoy, getting a bad vibe here, I was just trying to show that you can use a script in the head section.

To get back to you @jecommera: In my browser here it works with parentheses but maybe you need to use square brackets for 'normal' browsers:

elementStyle = document.images["image1"].style;

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Must be my fatherly instinct. When I see someone is trying to learn, I try to teach. Do it for them and they never learn. I have three very smart grown up children.
0
 

Author Closing Comment

by:jecommera
Comment Utility
yes I am practicing JavaScript so any of these I raise is to learn more so than get a solution that works.<br /><br />Got load of explanations above which help<br /><br />thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save signature to file 7 44
Cross domain request in Javescript 9 26
onOpen 14 38
Automatically open all pdf links in new tab 2 10
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now