Simple JavaScript Function Help

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Simple JavaScript Function Help

Post by jayshields »

I'm rubbish with JavaScript, don't understand why this won't work, or what the errors mean that FireFox gives me.

Code: Select all

function toggleHiddenElement(elementname) {
	if (document.getElementById(elementname).style.display == 'none') {
		document.getElementById(elementname).style.display = '';
	} else {
		document.getElementById(elementname).style.display = 'none';
	}
}
is my js function.

Code: Select all

<input type="checkbox" name="noimage" value="yes" onClick="toggleHiddenElement(hideme2)" />
is my function call.

Code: Select all

Error: document.getElementById(elementname) has no properties
Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
is what FireFox tells me.

I've tried a few variations of my code now and it's not having any of it.

Google doesn't help either.

Thanks people.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Sorry I cant help you personally but I can point you to this recent article http://www.dustindiaz.com/seven-togglers/ from Dustin Diaz about 7 ways to toggle / show/hide with javascript.
However, I just found out he's switching hosts so you'll have to look at the google cache page to see it at the moment.
(he's got much more great javascript articles by the way)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

onClick="toggleHiddenElement(hideme2)"
You're mssing quotes around hideme2. Javascript is trying to parse that as a variable which is undefined ;)

EDIT | Oh yeah and make sure you have id="hideme2" on the attributes for the element this refers to. name="hideme2" is wrong. Not sure if you've done that or if the error is being spooky :)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Phew! Thanks alot.

I'm slooooowwwwllllyyy learning this JavaScript lark.

Edit: Wwwwaaahhhh....

Code: Select all

Error: document.GetElementByID is not a function
Line: 8
:?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

GetElementById

lowercase d..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:GetElementById

lowercase d..
And lowercase "g". In JavaScript keywords start lowercase and change case for the first letter of each word:

getElementById()
getElementsByTagName()


The exception is the "Event" object. The reason for that is because "window" defaults to be the default top level object, and there's actually a window.event property already. "Event" is a top level object itself so this is needed to distinguish the two.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

heh yeah.. forgot that too. :P
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Thanks guys. I'll sort that out after the football :P

I just presume things are like they are in PHP, hence the case insensitive function name presumption.

I need to buy a book on JavaScript or something...!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jayshields wrote:I need to buy a book on JavaScript or something...!
http://www.amazon.co.uk/exec/obidos/ASI ... 50-7123925

That's the one the Open University send out. I have it and recommend it as a non-intimidating begginers book. And it's cheap ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As always, I can highly recommend O'Reilly's: http://www.oreilly.com/catalog/jscript4/index.html
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Definately go for:
http://domscripting.com/ by Jeremy Keith
and
DHTML Utopia: Modern Web Design Using JavaScript & DOM http://www.sitepoint.com/books/ by Stuart Langridge
if you want to know about the new unobtrusive cross-browser DOM scripting stuff.
Post Reply