Drop down selection changed event

13,612 views
Skip to first unread message

Kevin King

unread,
May 6, 2011, 10:05:04 AM5/6/11
to KnockoutJS
How does one handle the selection changed event for a single-select
drop down list?

I would imagine there is something we can bind it to like data-
bind="selectionChanged: changed" but I can't seem to figure out
exactly what it is.

Any help would be greatly appreciated!

Thanks,

Kevin

rpn

unread,
May 6, 2011, 10:16:48 AM5/6/11
to knock...@googlegroups.com
I am not sure about your exact scenario, but typically you would do something like this where you subscribe to an observable representing the value of the dropdown:

<select data-bind="options: choices, value: selectedChoice"></select>

var viewModel {
    choices["one""two""three"],
    selectedChoiceko.observable("two")
};

viewModel.selectedChoice.subscribe(function(newValue{
   alert("the new value is " newValue)
});


If you really need the event, then you could do something like:

<select data-bind="event: { change: selectionChanged }">
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>


var viewModel {
    selectionChangedfunction(event{
         alert("the other selection changed");  
    
};


Here is a sample with both: http://jsfiddle.net/rniemeyer/LBz6d/

 

Kevin King

unread,
May 6, 2011, 11:19:54 AM5/6/11
to KnockoutJS
Thanks dude, another great suggestion!

anshu...@gmail.com

unread,
Feb 18, 2014, 1:00:59 AM2/18/14
to knock...@googlegroups.com, kevi...@gmail.com
But how do i get which element had changed?

anshu...@gmail.com

unread,
Feb 18, 2014, 1:01:55 AM2/18/14
to knock...@googlegroups.com
But how do i get which element had changed?

Reply all
Reply to author
Forward
0 new messages