[thelist] How to abstract this script? [solved]

Tom Dell'Aringa pixelmech at yahoo.com
Wed Mar 9 13:26:09 CST 2005

--- Chris Heilmann <lists at onlinetools.org> wrote:
> passednode is an object name matt came up with.

Yeah, I told Matt I realized that a nanosecond after I hit 'send'...bah.

> Did you take a look at the unobtrusive course example I posted which
> explains _in detail_ exactly what you want to do?

Yep, I've seen that before. I reread that, but it's not what I want to do. He's showing you how to
dynamically build JS so its unobtrusive. My real issue was having my  input object abstracted so I
could get multiple uses on the same page. I don't care that my javascript call is hardcoded. (Nor
does he have an actual date picker script - he just used that as an example for building a link).

> your
> > var oNodes = oLink.childNodes;
> > ShowCalendar(oNodes[0].value, oNodes[0]);
> can choke on whitespace in Mozilla.

Can you elaborate on that? Where is the whitespace that chokes it? In any event (I replaced that
line so might not be an issue), here's what I did based on what Matt Warden suggested:

1. Wrapped the input and the anchor in a div. This allowed me to only have the image as a link,
not the input.

2. Sent 'this.parentNode' as an object reference to my init function, which gives me the div

3. Got an array of childNodes from the parent, of which I know my input is one. Because IE and
Gecko will grab a first different node (IE grabs the input, Gecko grabs some nebulous text node) I
had to write a little utility function to match the first occurance of a type1 node (an element)
so I know I have the input for all browsers. This little test:

if(divObjects[i].nodeType == 1)

makes sure I have the right guy. Setting him to a global variable 'currentDateInput' allows me to
toss the object around to each function without calling it by name. 

Now I can call my script from 4 different places on the page without ever naming an element. They
all use the same calander div too for the picker, so there's only one div per page. So naming the
picker div is not an issue.

You can see it working at:


Feel free to test it for me and let me know if anything breaks. If anyone sees any further
improvements I could make please feel free to suggest them, I'm sure it's far from perfect, my JS
is rusty lately.



Melissa: Ace, Where are you?
Ace Ventura: I'm in Psychoville and Finkle's the Mayor.

More information about the thelist mailing list