views:

687

answers:

1

I am using the jQuery plugin found here http://dropdown-check-list.googlecode.com/svn/trunk/demo.html

Everything works great except when I need to populate the field on load for an edit page.

So let's say I am using this control on a profile page. On the profile, the user can select many User Types. So when they create a new account, they can select as many user types as they want, but when they come back to edit their profile, how do I set the initial box to have those user types they selected in it when the page loads?

Any suggestions?

+1  A: 

According to the documentation, the items will be "checked" upon widget initialization if the corresponding <option> has the selected attribute. Something like:

<select id="UserTypes">
    <option value="val1" selected="selected">User Type 1</option>
</select>

You can apply the selected attribute via jQuery if you wish to avoid doing it at the server for some reason.

Ken Browning
Thanks for the answer! Do you happen to know how to get the answers selected from jQuery afterward? I'd like to be able to get the Text and Value of the options selected in javascript. There will be no postback on my page.
vipergtsrz
I assume that the widget sets/unsets the `selected` attribute whenever an item is checked/unchecked. If my assumption is correct then you can select the selected options with this selector: `$('#UserTypes option:selected')`. From there you can get values by inspecting `.attr('value')` and text by inspecting `.text()`
Ken Browning