Forum: Rails Spinoffs (closed, excessive spam) CSS selectors and Prototype 1.6

Posted by yoda230 (Guest)
on 2008-06-24 15:03
(Received via mailing list)
I have a number of select form elements all with a class "extrainfo".
What I would like to do is to capture when the select form element is
changed and run a function passing the select form elements ID/Name.
What is the best way to do this?

$$('.extrainfo').Event.observe('change',myfuntction(id))
Posted by Luca Guidi (Guest)
on 2008-06-24 15:12
(Received via mailing list)
DollarDollar returns a collection of matched elements, so the best way 
is:
$$('.extrainfo').each(function(element){
   Event.observe(element, 'change', myfuntction(id))
});

http://prototypejs.org/api/enumerable/each

--
blog: www.lucaguidi.com
Pro-Netics: www.pro-netics.com
Sourcesense - making sense of Open Source: www.sourcesense.com
Posted by SWilk (Guest)
on 2008-06-24 15:47
(Received via mailing list)
Hi,

Luca Guidi wrote:
> DollarDollar returns a collection of matched elements, so the best way is:
> $$('.extrainfo').each(function(element){
>    Event.observe(element, 'change', myfuntction(id))
> });
> 


Top of my head and not tested,
you could also use #invoke, which is simpler:


$$('.extrainfo').invoke('observe', 'change', myfunction);


Notice that you should not execute myfunction(),  but pass reference
to it to #observe method (unless myfunction(id) returns reference to a
function).

myfunction will be called with event as first parameter, so you will
have to check Event.findElement() to check which <select> was changed,
e.g:

function myfunction(evnt) {
   //should alert id of changed  <select>
   alert (Event.findElement(evnt).id);
}

See Enumerable#invoke docs for details.

--
Regards,
SWilk
This topic is locked and can not be replied to.