trigger a 'change' event for a 'select' element

4,104 views
Skip to first unread message

Anjanesh

unread,
Jan 29, 2009, 10:18:19 AM1/29/09
to Prototype & script.aculo.us
I need to trigger a 'change' event for a 'select' element.

alert($($(elem).parentNode)); // shows [Object HTMLSelectElement]
$($(elem).parentNode).fire('change'); // How do I indicate which
option value to change to ?

But the fire() doesnt work. But no JS errors either.

this.settings.each(function(element){
Event.observe(element, 'change', this.configure.bind
(this))
}.bind(this));

configure: function(event) { alert(event);
var element = Event.element(event);
this.configureElement(element);
},

If the fire() worked,then alert(event); would've shown something.

T.J. Crowder

unread,
Jan 29, 2009, 10:29:42 AM1/29/09
to Prototype & script.aculo.us
Hi,

The documentation of Element#fire (here[1]) actually makes a point of
saying that it can't be used to fire native events.

[1] http://prototypejs.org/api/element/fire

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

Peter De Berdt

unread,
Jan 29, 2009, 10:35:40 AM1/29/09
to prototype-s...@googlegroups.com

On 29 Jan 2009, at 16:18, Anjanesh wrote:

I need to trigger a 'change' event for a 'select' element.

alert($($(elem).parentNode)); // shows [Object HTMLSelectElement]
$($(elem).parentNode).fire('change'); // How do I indicate which
option value to change to ?

But the fire() doesnt work. But no JS errors either.


Element#fire does not support firing native events. All custom event names must be namespaced (using a colon). This is to avoid custom event names conflicting with non-standard native DOM events such as mousewheel and DOMMouseScroll.

What you want to to, is set the selectedIndex value from the <select> element as is documented at http://www.w3schools.com/htmldom/dom_obj_select.asp
selectedIndex needs to be an integer. If you want to select it by value, then you'll need to loop the values and when you find the right one, check what index it has (selectedIndex is a getter and a setter) and set it to that index.


Best regards


Peter De Berdt


david

unread,
Jan 30, 2009, 5:59:55 AM1/30/09
to Prototype & script.aculo.us
Hi Anjanesh,

I use a simple bypass to this limitation:
when observing an event, if it's a native event, I create a custom
event "native:click" that reference the same call back to the original
click observer.
So firing this "native:click" event, does the job.

--
david

On 29 jan, 16:35, Peter De Berdt <peter.de.be...@pandora.be> wrote:
> On 29 Jan 2009, at 16:18, Anjanesh wrote:
>
> > I need to trigger a 'change' event for a 'select' element.
>
> > alert($($(elem).parentNode)); // shows [Object HTMLSelectElement]
> > $($(elem).parentNode).fire('change'); // How do I indicate which
> > option value to change to ?
>
> > But the fire() doesnt work. But no JS errors either.
>
>  Fromhttp://prototypejs.org/api/element/fire:
> Element#fire does not support firing native events. All custom event  
> names must be namespaced (using a colon). This is to avoid custom  
> event names conflicting with non-standard native DOM events such as  
> mousewheel and DOMMouseScroll.
>
> What you want to to, is set the selectedIndex value from the <select>  
> element as is documented athttp://www.w3schools.com/htmldom/dom_obj_select.asp

Lars Schwarz

unread,
Jan 30, 2009, 6:58:22 AM1/30/09
to prototype-s...@googlegroups.com
what's wrong with a simple (regular, no-need-for-external-libraries)
"onChange" like:

<select id="getmethisselectindex" name="getmethisselectindex"
onChange="myJsFunc();">

and in myJsFunc: var myid = $F('getmethisselectindex');

or did i miss something?
--
Lars Schwarz
Heiligengeiststr. 26
26121 Oldenburg
T 0441 36110338
M 0151 1727 8127
W www.bitrocker.com

david

unread,
Jan 30, 2009, 7:28:50 AM1/30/09
to Prototype & script.aculo.us
Hi Lars,

depending on the context, the DOM 0 events is still there and
prototype does not break it so you're totally right to remember
everybody it's still alive!
But some could cry to offense, because of MVC .... but stop, it could
launch a discution I did not want.

--
david
Reply all
Reply to author
Forward
0 new messages