473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Passing a DIV ID to a function

I am embarrassed that I cannot figure this out, as it seems fairly
simple.

I found a JS function I'd like to use, which animates a layer so that
it moves across the screen.

The problem is the function, as found, simply names the DIV directly. I
want to be able to use the function in the HTML to pass the ID of the
DIV which gets moved. Sounds fairly straight-forward, right? Well it
doesn't work and I have spent HOURS on this. Yes, I am red-faced.

==========================
The HTML, as found:

<div id = "myDiv" style="position:absolute; top:250px;
left:100px;"><img src="bee_right.gif"></div>

The function call, as found:

<a href="#" onClick="the_timeout=setTimeout('moveDiv();',100); return
false;">start moving!</a>

The function (two of them actually), as found:

var the_timeout;

function moveDiv()
{
// get the stylesheet
//
var the_style = getStyleObject("myDiv");
if (the_style)
{
// get the current coordinate and add 5
//
var current_left = parseInt(the_style.left);
var new_left = current_left + 5;

// set the left property of the DIV, add px at the
// end unless this is NN4
//
if (document.layers)
{
the_style.left = new_left;
}
else
{
the_style.left = new_left + "px";
}

// if we haven't gone too far, call moveDiv() again in a bit
//
if (new_left < 400)
{
the_timeout = setTimeout('moveDiv();',10);
}
}
}
function getStyleObject(objectId) {
// cross-browser function to get an object's style object given its
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId).style;
} else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId).style;
} else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
} else {
return false;
}
} // getStyleObject
==================

OK. It seemed to me that all I needed to do was rewrite the function
call:

<a href="#" onClick="the_timeout=setTimeout('moveDiv('myDiv'); ',100);
return false;">start moving!</a>

And then re-write the function (I'm showing only the parts I changed):

function moveDiv(divID)
{
// get the stylesheet
//
var the_style = getStyleObject(divID);
===================

so that I am passing the name of the div in the function call, the
function moveDiv is set up to receive the divID argument, and then it
passes that argument to the getStyleObject function where it is used to
access the style that I want to manipulate, depending on the flavor of
the browser.

Why doesn't this work? Thanks in advance for your help! I feel so
stupid that I cannot figure this out...

Dec 4 '06 #1
6 7268
I would like to add that I did try using 'this':

<a href="#" onClick="the_timeout=setTimeout('moveDiv(this);',1 00);
return false;">start moving!</a>
along with:

function moveDiv(objectID)
{
// get the stylesheet
//
var the_style = getStyleObject(objectID);
and that doesn't work either.

Dec 4 '06 #2
ry****@gmail.com wrote:
OK. It seemed to me that all I needed to do was rewrite the function
call:

<a href="#" onClick="the_timeout=setTimeout('moveDiv('myDiv'); ',100);
return false;">start moving!</a>

And then re-write the function (I'm showing only the parts I changed):

function moveDiv(divID)
{
// get the stylesheet
//
var the_style = getStyleObject(divID);
===================

so that I am passing the name of the div in the function call, the
function moveDiv is set up to receive the divID argument, and then it
passes that argument to the getStyleObject function where it is used to
access the style that I want to manipulate, depending on the flavor of
the browser.

Why doesn't this work? Thanks in advance for your help! I feel so
stupid that I cannot figure this out...
Your overall idea is correct. You just have a syntax error in your
anchor tag.
<a href="#" onClick="the_timeout=setTimeout('moveDiv(\'myDiv\' );',100);
return false;">start moving!</a>

When you have quotes within quotes within quotes you'll eventually have
to start escaping them so javascript can tell the difference between
real quotes and "part of the string" quotes. In this case all you have
to do is escape the quotes around myDiv.

--
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 4 '06 #3
ry****@gmail.com wrote:
<a href="#" onClick="the_timeout=setTimeout('moveDiv('myDiv'); ',100);
return false;">start moving!</a>
You need to use different quotes and in the HTML attribute you can use
&quot; to escape the double quote:
<a href="#"
onclick="the_timeout = setTimeout('moveDiv(&quot;myDiv&quot;);',
100); return false;">

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 4 '06 #4
thanks for the timely response. This worked! Problem is now it doesn't
work quite as intended. The DIVD should move to the absolute position
of 400px left in 5px increments, and stop. What it does instead is move
in 5px increments per click, period.

Here is the troublesome part of the function:

if (new_left < 400)
{
the_timeout = setTimeout('moveDiv();',10);
}

If I change the argument for moveDiv to the actual name of the div, it
works as intended. But if I do

the_timeout = setTimeout('moveDiv(divID);',10);

or

the_timeout = setTimeout('moveDiv(this);',10);

it doesn't. Now what's the problem?
pcx99 ha scritto:
ry****@gmail.com wrote:
OK. It seemed to me that all I needed to do was rewrite the function
call:

<a href="#" onClick="the_timeout=setTimeout('moveDiv('myDiv'); ',100);
return false;">start moving!</a>

And then re-write the function (I'm showing only the parts I changed):

function moveDiv(divID)
{
// get the stylesheet
//
var the_style = getStyleObject(divID);
===================

so that I am passing the name of the div in the function call, the
function moveDiv is set up to receive the divID argument, and then it
passes that argument to the getStyleObject function where it is used to
access the style that I want to manipulate, depending on the flavor of
the browser.

Why doesn't this work? Thanks in advance for your help! I feel so
stupid that I cannot figure this out...

Your overall idea is correct. You just have a syntax error in your
anchor tag.
<a href="#" onClick="the_timeout=setTimeout('moveDiv(\'myDiv\' );',100);
return false;">start moving!</a>

When you have quotes within quotes within quotes you'll eventually have
to start escaping them so javascript can tell the difference between
real quotes and "part of the string" quotes. In this case all you have
to do is escape the quotes around myDiv.

--
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 4 '06 #5
I have tried this syntax also, based on examples seen elsewhere:

the_timeout = setTimeout('moveDiv('+divID+');',10);

this does not work either.

ry****@gmail.com ha scritto:
thanks for the timely response. This worked! Problem is now it doesn't
work quite as intended. The DIVD should move to the absolute position
of 400px left in 5px increments, and stop. What it does instead is move
in 5px increments per click, period.

Here is the troublesome part of the function:

if (new_left < 400)
{
the_timeout = setTimeout('moveDiv();',10);
}

If I change the argument for moveDiv to the actual name of the div, it
works as intended. But if I do

the_timeout = setTimeout('moveDiv(divID);',10);

or

the_timeout = setTimeout('moveDiv(this);',10);

it doesn't. Now what's the problem?
pcx99 ha scritto:
ry****@gmail.com wrote:
OK. It seemed to me that all I needed to do was rewrite the function
call:
>
<a href="#" onClick="the_timeout=setTimeout('moveDiv('myDiv'); ',100);
return false;">start moving!</a>
>
And then re-write the function (I'm showing only the parts I changed):
>
function moveDiv(divID)
{
// get the stylesheet
//
var the_style = getStyleObject(divID);
===================
>
so that I am passing the name of the div in the function call, the
function moveDiv is set up to receive the divID argument, and then it
passes that argument to the getStyleObject function where it is used to
access the style that I want to manipulate, depending on the flavor of
the browser.
>
Why doesn't this work? Thanks in advance for your help! I feel so
stupid that I cannot figure this out...
Your overall idea is correct. You just have a syntax error in your
anchor tag.
<a href="#" onClick="the_timeout=setTimeout('moveDiv(\'myDiv\' );',100);
return false;">start moving!</a>

When you have quotes within quotes within quotes you'll eventually have
to start escaping them so javascript can tell the difference between
real quotes and "part of the string" quotes. In this case all you have
to do is escape the quotes around myDiv.

--
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 4 '06 #6
ASM
ry****@gmail.com a écrit :
I have tried this syntax also, based on examples seen elsewhere:

<div id = "myDiv" style="position:absolute; top:250px;
left:100px;"><img src="bee_right.gif"></div>

The function call :
To slide your div 'myDiv' from its position to right at 500px more with
a fast speed. (increase 10 to slow the slide)

<a href="#" onClick="moveDiv('myDiv',10,500); return
false;">start moving!</a>

So you can also slide slowly the div on a short distance :

<a href="#" onClick="moveDiv('myDiv',70,50); return
false;">moving shortly</a>

The function :

function moveDiv(theDiv, tempo, distance, start)
{
// get the stylesheet
//
var the_style = getStyleObject(theDiv);
if (the_style)
{
// get the current coordinate
var current_left = parseInt(the_style.left);

// set point of starting
if(typeof(start)=='undefined' || start==null) start = current_left;

// and add 5 to the current coordinate
var new_left = current_left + 2;

// set the left property of the DIV, add px at the
// end unless this is NN4
the_style.left = (document.layers)? new_left : new_left+'px';

// if we haven't gone too far, call moveDiv() again in a bit
if (new_left < (+distance+start))
the_timeout = setTimeout(function(){
moveDiv(theDiv, tempo, distance, start)
}, tempo);
}
}
function getStyleObject(objectId) {
// cross-browser function to get an object's style object given its
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId).style;
} else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId).style;
} else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
} else {
return false;
}
} // getStyleObject

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.