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

How do you manually set the select2 value? #27

Closed
sferoze opened this issue Mar 10, 2016 · 6 comments
Closed

How do you manually set the select2 value? #27

sferoze opened this issue Mar 10, 2016 · 6 comments

Comments

@sferoze
Copy link

sferoze commented Mar 10, 2016

I have been trying to do this simple thing with this component and it does not work.

In the onSelect method, I want to return the value to the previous selected value.

So I am trying to manually set the select2 value.

$('.select-input').val('theValue')

but setting it with .val function does not work

Also using the .select2 method does not work because it does not exist.

How can I manually set the select2 value with this component?

@sferoze
Copy link
Author

sferoze commented Mar 10, 2016

Basically, in the onSelect function, I want to show a confirm modal, and if they do not confirm, I want to return the select value to what it was previously.

I have been trying to do this for looong time and tried many different names using .val

Please can you help me. Do you have an example of how to manually set the select value of this component. I use this component everywhere in my app

@rkit
Copy link
Owner

rkit commented Mar 10, 2016

  1. Set ref for component

    <Select2 ref="tags" />
  2. Now, you can access to select2

    this.refs.tags.el.val('discussion').trigger('change'))

    https://github.com/rkit/react-select2-wrapper#access-to-select2

@rkit
Copy link
Owner

rkit commented Mar 10, 2016

But in this case, the component properties are not updated.
You can change value using prop value

@sferoze
Copy link
Author

sferoze commented Mar 10, 2016

@rkit Thank you soo much!

this.refs.tags.el.val('discussion').trigger('change'))

Works!

I would have never figured it out otherwise. Can you add exactly that post in the documentation under, "How to programmatically update select2"

Thanks so much for the quick reply, now I can continue!

@rkit
Copy link
Owner

rkit commented Mar 10, 2016

Ok, tomorrow I will add.
Can I close issue?

@sferoze
Copy link
Author

sferoze commented Mar 10, 2016

Ill close it! thanks!

@sferoze sferoze closed this as completed Mar 10, 2016
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