Closed Thread Icon

Topic awaiting preservation: variable string including ' ' Pages that link to <a href="https://ozoneasylum.com/backlink?for=8525" title="Pages that link to Topic awaiting preservation: variable string including &amp;#039; &amp;#039;" rel="nofollow" >Topic awaiting preservation: variable string including &#039; &#039;\

 
Author Thread
Lord_Fukutoku
Paranoid (IV) Inmate

From: West Texas
Insane since: Jul 2002

posted posted 02-11-2003 22:27

Well, I was bored at work the other day, so I started working on this javascript to pass the time... Everything went fine until I tried passing parameters to the function...

What I have is this essentially (just the relevent parts at least):

code:
function loadMenu(toLoad)
{
var quote = "'";
var toChange = quote+toLoad+quote;
//other code here...
loadMenu2(toChange);
}
function loadMenu2(toLoad)
{
var toChange2 = toLoad;
//document.write(toChange2); /* Test to make sure the single quote stayed with the string... It worked as it should... */

document.getElementById(toChange2).style.posLeft += 10;
/* More code here... */
}

<!-- //End HEAD, start BODY -->

<div id="divA" onmouseover="loadMenu('divA1')">


The part that is giving the problem is this line in the second function:
document.getElementById(toChange2).style.posLeft += 10;

It works if I write out a seperate function for each possible variable called, and have this line instead:
document.getElementById('divA1').style.posLeft += 10;

The problem seems to arise when I use the variable toChange2, which includes the single quotes, in place of the actual string.

When I tested to see if the variable kept the quotes by outputting the string, it worked and outputted whatever string was sent as a parameter along with the quotes. So it made it that far at least.

I've also tried using the escape character with the quotes in the getElementById() call...


Any ideas what I might be missing here?


-- Unoriginal Cell 693 --

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-11-2003 23:19

I think you misunderstand the role that quotes play in programming languages. Understand that when we do this:

document.getElementById('someid')

we're not passing in the string

'someid'

to the function. What we're passing is the string

someid

. The only purpose of the quotes is to let the browser know that it's a string and not a variable name.

If however, it is a variable name, as in your case, no quotes need to be used at all. You should just pass in the variable, and it will take its string value, and search for an element with that ID.

So the line

var toChange = quote+toLoad+quote;

is unnecessary and should simply be

var toChange = toLoad;

or the toChange variable should be removed altogether.

Lord_Fukutoku
Paranoid (IV) Inmate

From: West Texas
Insane since: Jul 2002

posted posted 02-12-2003 03:17

Yea, that's what I figured, and that's what I started with actually. When it didn't like it, I played with it a bit until I ran out of ideas, then asked someone else that works in the lab and the only thing we could come up with is to add the quotes, and, well, you can see how well that worked...

I'll keep playing with it until it throws a new twist at me, or I happen across something that works.

Thanks for at least narrowing the options down though Slime.


[edit: The only error I can get it to throw is : Object required, in the same line as before: document.getElementById(toLoad).style.posLeft += 10;

It will increment the div's position by 10, like it's supposed to, but it doesn't make it to the next line...

[This message has been edited by Lord_Fukutoku (edited 02-12-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-12-2003 03:37

If you throw up a page demonstrating the problem, I'd be happy to look at it.

Lord_Fukutoku
Paranoid (IV) Inmate

From: West Texas
Insane since: Jul 2002

posted posted 02-12-2003 03:55

All righty, I took the liberty of putting it up on the lab's server (perk of working here... Except it only allows me to edit it in FrontPage, blah... )
Link: http://chimera.cslab.utpb.edu/practice/menubeta.htm


[edit: Changed link]

[This message has been edited by Lord_Fukutoku (edited 02-12-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu