File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Is it better practice to pass an element object or element id to a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Is it better practice to pass an element object or element id to a function" Watch "Is it better practice to pass an element object or element id to a function" New topic
Author

Is it better practice to pass an element object or element id to a function

C Brown
Greenhorn

Joined: Jun 24, 2009
Posts: 16

Should I pass an element object to a function or just the element id? I ask because I am having trouble getting the following to work (I'm trying to write a function that will either fade or display a div)...


The original function call is


When I run this code I get this error, "missing ] after element list" (in my example its line 5). When I pass in an element object instead of an element name I get this error, "el.style is undefined". Obviously I'm doing something wrong since neither one of them work, but if you could tell me which way is the "better" way or if it really doesn't matter. Thanks in advance.
C Brown
Greenhorn

Joined: Jun 24, 2009
Posts: 16

I managed to get my function to work by fixing the setTimout function.

But if somebody could still tell me the better practice it would be much appreciated.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
look how this string is built if el = "foo";



renders as:



so that means you are missing quotes around your string.

Better solution is to use a closure



Reason why a closure is better is you do not have to worry about quotes. The main reason you get better performance. When you use a string, the string needs to be evalutated. When you pass in a function reference, there is no evaluation step.

Eric
C Brown
Greenhorn

Joined: Jun 24, 2009
Posts: 16

I've never heard of closures before so I had to google it, they seem a little confusing. I've found some tutorials so I'll give them a shot and see how it goes. Thanks for the tip.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Closures are not that hard, people make them seem harder than they are. If you want to do advanced JavaScript coding, you need to know closures.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Closures and function contexts are essential concepts to writing good script, yet most page authors have no idea how they work.

They are such an important concept that I dedicated an entire appendix to explaining them in jQuery in Action. If you know someone with a copy of the book, it's worth reading.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
C Brown
Greenhorn

Joined: Jun 24, 2009
Posts: 16

Thanks, I'll check it out.
 
Don't get me started about those stupid light bulbs.
 
subject: Is it better practice to pass an element object or element id to a function
 
Similar Threads
How to make table visible on radiobutton state
form element -- objeect null
Options Tag not Recognizing Collection
document.forms[0].elements.focus() is not working in IE 7.0+ and FF 3.0
prototype selector