Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to select an option from JS ? #236

Closed
softshape opened this issue Aug 29, 2014 · 4 comments
Closed

Correct way to select an option from JS ? #236

softshape opened this issue Aug 29, 2014 · 4 comments
Assignees

Comments

@softshape
Copy link

Hi all,

simple $('#myselect').val('value1') won't work if #myselect is wrapped with DropKick.

Strange but $('#myselect').val('value1').dropkick('refresh') won't work also.

What is the correct way to change selected value?

@wwilsman
Copy link
Collaborator

If you are using the jQuery wrapper, $('#myselect').dropkick('select', index) should work; where index is the index of the item you are trying to select. A future update will allow a value to be passed optionally.

Native equivelent: Dropkick('#myselect').select(index).

If you absolutely need to select an element by it's text value, then you can use the search function in conjunction with the select function.

var dk = Dropkick('#myselect');
var matches = dk.search('value1');
matches && dk.select(matches[0]);

@softshape
Copy link
Author

Thank you, Wil!

The 'text value' is the best way because it's close to how .val() works in jQuery. I don't know if DropKick can listen to change() event of the select - if yes, an ideal implementation would look like -

$('#myselect').val('value1')

Yes, no DropKick in this line. DropKick would listen to the change() and then update its current value automatically.

@wwilsman
Copy link
Collaborator

wwilsman commented Sep 1, 2014

Agreed. The next update will most likely include a listener on the select to update the Dropkick. For now, I'm going to leave this issue open and get this implemented later in the week.
.

@wwilsman wwilsman self-assigned this Sep 1, 2014
@wwilsman
Copy link
Collaborator

wwilsman commented Sep 6, 2014

Can't listen to change event in native JavaScript. Instead it is now possible to select an option based on it's value like so:

Dropkick('#myselect').select('value1')
or
$('#myselect').dropkick('select', 'value1').

@wwilsman wwilsman closed this as completed Sep 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants