every event handler gets the event that calls it as an argument. for
example,

function handler(event) {
do something;
}

element.observe('click', handler);

whenever you click that element, your handler will have an Event
object, named event in this case. The Event object has a number of
properties, one of which will be the target of the even, i.e. the
element that was clicked on. Using this, you can get identifying
information about the element.
Or you could just use the prototype function Event.element, which gets
the calling element for you.


Also, to avoid mucking about with event handler management, you can
just drop a handler on the container div. In the handler, you check to
see if that element that was clicked is one you want to do something
with.

Look up event bubbling. Someone else can explain it far better than I.
Since discovering it, it's made my life so much easier.

On Sep 6, 5:38 pm, elduderino <[EMAIL PROTECTED]> wrote:
> Yeah agreed....so if they're inserted on completion of an xhr request
> then you can just use the callback of this and run my code (without
> the dom:loaded)....i don't think you need the inline event handlers
> here
>
> On Sep 5, 6:10 pm, Matt Foster <[EMAIL PROTECTED]> wrote:
>
> > Problem with that idea Elduderino would be that if the list elements
> > aren't loaded upon dom:loaded but inserted upon the completion of an
> > XHR request, which I assume to be true based on the execution of
> > Element.update.
>
> > Two ways to roll with this, i prefer the latter...
>
> > Inline click handlers as you had mentioned, inside the quotes of the
> > event handler two variables are available that will assist in the
> > reference of the list element, this and event.  Event will contain a
> > reference to the object and this is a reference to the object so it
> > would look something like this...
>
> > <li onclick="someFunc(this, event);">Hooray</li>
>
> > By using Element.update you must have a reference to the parent object
> > in which these list items are getting inserted into.  After insertion,
> > iterate over a collection derived from the object and attach the
> > listeners through that.
>
> > Element.update(ele, t.responseText);
>
> > ele.getElementsBySelector("li").invoke("observe", "click", someFunc);
>
> > On Sep 5, 10:12 am, elduderino <[EMAIL PROTECTED]> wrote:
>
> > > why not just grab all the links as sson as the dom is loaded....then
> > > observe all these links and when one is clicked you can access it
> > > using this.id...something like:
> > > document.observe("dom:loaded", function() {
> > > var links = $$('.links');
> > >        links.each(function(w) {
> > >            w.observe('click', function(e) {
> > >                var linkId = this.id;
>
> > > //etc
>
> > > })
> > > })
>
> > > This grabs all the links with a class of links.....yes each link would
> > > need it's own id...but if you're generating the html from a db anyways
> > > then what's the problem?! You can do this in a seperate javascript doc
> > > so theres no inline event handlers needed
>
> > > On Sep 4, 10:42 pm, BrentNicholas <[EMAIL PROTECTED]> wrote:
>
> > > > Hi all,
>
> > > > I'm building an application and am unsure of how to do this. What I
> > > > came up with is too complex to be correct/best practice.
>
> > > > Overview:
> > > > I have a list (HTML fragment) that is dropped into a DIV via
> > > > Element.update
>
> > > > This list changes each time you click a link in the application.
>
> > > > I need to be able to click on an item in the list and know what item
> > > > was clicked. I can create this list (HTML fragment) however I need, so
> > > > placing the record ID in each list item isn't a problem.
>
> > > > What is the problem is: How do I know what list item was clicked?
>
> > > > I thought about making each list item:
>
> > > >    ** an anchor tag with an onclick function call, passing the param.
> > > > (Con: has js inline in the page)
>
> > > >    ** an 'id' of the db record ID, but then what do you link the
> > > > Event.observe('listElementID','click',functionName) to?
>
> > > > Does this make sense? I know people have had to do this before.
> > > > In short: have an HTML list of items, that is injected into the page
> > > > based on some selection, then have all list items fire a common
> > > > function on click, but know what one was clicked.
>
> > > > Thanks for your time on this,
> > > > Brent Nicholas
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to