Changing select selectedIndex in code...

1,334 views
Skip to first unread message

greg....@gmail.com

unread,
Mar 14, 2012, 3:24:25 PM3/14/12
to knock...@googlegroups.com
I'm using jQuery to update the selected value of a data-bound select drop-down [  $(item).val(newVal)  ], and the underlying bound KO data is not updating.  Is there a way to get the bound data to update as well?

The javascript code that is updating the selectedIndex doesn't have knowledge of the data model.

Thanks,
Greg.

John ODonnell

unread,
Mar 14, 2012, 10:14:14 PM3/14/12
to KnockoutJS
Have you tried using an event binding? It would look something like

'data-bind'=>"event: {change: doSomething}"

I forgot if it's onchange (JS) or change (jQuery). Either way, that
would register the doSomething function to act on the change event.
You could have doSomething grab the field value and set KO explicitly.

See Note 2 at http://knockoutjs.com/documentation/event-binding.html
for some examples.

Also, check to see if 'valueUpdate' is set to 'change' (it's the
default.)

greg....@gmail.com

unread,
Mar 16, 2012, 8:33:24 AM3/16/12
to knock...@googlegroups.com
Thanks for the suggestion John.  I will try that today.

greg....@gmail.com

unread,
Mar 16, 2012, 12:32:34 PM3/16/12
to knock...@googlegroups.com
Well, I didn't actually use what you suggested, but you certainly gave me a good idea.  When setting the selectedIndex of select box with JavaScript, the change event isn't fired (at least it isn't in Chrome).  So, after I set the selected index, I fire the change event with jQuery and KO then updates accordingly.

Thanks for your help.

On Friday, 16 March 2012 08:33:24 UTC-4, greg....@gmail.com wrote:
Thanks for the suggestion John.  I will try that today.

ha1...@gmail.com

unread,
Apr 29, 2014, 2:16:36 AM4/29/14
to knock...@googlegroups.com, greg....@gmail.com
Hi Greg, i'm actually having the same exact issue as you...i'm using jQuery to update the selected value of a data-bound select dropdown, but KO data doesn't update/bind the underlying data - what method did you use with jQuery to update KO to appropriately bind the underlying data?
Reply all
Reply to author
Forward
0 new messages