Why does this return FALSE???? ARRRGGGHH

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Why does this return FALSE???? ARRRGGGHH

Post by JayBird »

here my javascript function

Code: Select all

<SCRIPT>
				function showInfo(layerName)
				&#123;
				var layerName;
				
					if(layerName == "Engines")
					&#123;
						document.write('TRUE');
					&#125; else &#123;
						document.write('FALSE');
					&#125;
				&#125;
				</SCRIPT>
Here is the link that calls it

Code: Select all

<a href="javascript:showInfo(Engines);">Latest John Deere PowerTech Engines</a>
Why does it return FALSE??

Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Because you have redefined the variable:

Code: Select all

var layerName;
Remove this line, it should solve your problem.
Just my guess...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

nope, still returns false
I think it is something to do with layerName being an object for some reason.

I really don't know enough about JS tho to figure it out

Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

<SCRIPT> 
    function showInfo(layerName) 
    &#123; 
        if(layerName == "Engines") 
        &#123; 
             document.write('TRUE'); 
        &#125; else &#123; 
             document.write('FALSE'); 
        &#125; 
    &#125; 
</SCRIPT> 
	
<a href="javascript:showInfo('Engines');">Latest John Deere PowerTech Engines</a>
Notice the quotes in function call.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Okay, i c

so how do i use the variable layerName when referencing an object

I wanna change the content on the DIV called "Engines". You would normally do it like this

Code: Select all

Engines.innerHTML = "It worked";
but, there will be different layers on the page, so i wanna replace Engines with a variable, i thought this would work, but doesm't

Code: Select all

layerName.innerHTML = "It worked";
Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

If you want to pass string to the function:

Code: Select all

<script type='text/javascript' language='JavaScript'> 
    function showInfo(layerName) 
    &#123;
       var q = document.getElementById(layerName);
       q.innerHTML="It works!";
    &#125;
</script>
<a href="javascript:showInfo('Engines');">Latest John Deere PowerTech Engines</a>
<div id='Engines'></div>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

thank f*ck for that, been tearing my hair out with this.

I've decided i don't like JavaScript :)

Cheerz buddy

Mark
Post Reply