When trying to use an icon for an option it seems to be passed as JSX in the onChange handler. Anyway to get hold of original label in option prior to injecting image in the onChange handler? Or is there a way to get hold of index of selected option.
Here's a sample https://codesandbox.io/s/react-select-label-icon-forked-52djv
Using formatOptionWithImage solves the problem.
Example
https://codesandbox.io/embed/reactselect-formatoptionlabel-bde1q
Related
I have an input text field, which value is stored in "app.adress" component in Vue. I'm now using "Easy-autocomplete" plugin, which displays a list of suggested values. When I click one it appears in the text field, but is not saved in "app.adress" component. For example, I type "uni", I click suggested value "United Kingdom", it appears in the text field, but only "uni" is saved in the "app.adress". Any suggestions how to update Vue component by click?
I know this answer is too late. But I just want to share my work around on this issue. Instead of using v-model to bind the value of my input, I just add #change event listener and call a method that will update my data.
Edit: Other solution is to use the event functions of EasyAutocomplete like the onChooseEvent.
How to reset selected value in yii2 widget select2 using jQuery or JavaScript? I've tried to use $("#select2id").val("").trigger("change"); and it show the placeholder, but the first item is always automatically selected.
EDIT: I'm using this widget for make dependency dropdown. the widget that the first item always automatically selected is the dependent widget.
You should do it the select2 way:
$("#select2id").select2("val", "", true);
With the first two parameters you assign an empty string as a value and the last one is for triggering the change.
I've solved the problem. That code is actually working. The order of the code that make the problem. So, be sure to check the code order.
I am building a search tool with various select drop downs that populate with options via AJAX. The possible options populated are based on the the option chosen in the preceding select drop down. For the purpose of this tool, I want to have the first select box hidden but still need to select an option in that box so that it triggers the AJAX call on the following box, something that is supposed to happen as the result of an "onchange" event.
I've tried all kinds of different code to simulate a mouse click selection of a particular option but, while I can get the option selected, it still isn't triggering the event properly to set off the AJAX call in the following select. This is as far as I've gotten:
jQuery('#form select').first().val('the-value').trigger('click').trigger('change');
From everything I've read, that should set the option value and trigger a change event much like clicking the option. Still, this isn't working. Thanks!
You only have to use val(), like any other field...
$("select").val("2").change();
http://jsfiddle.net/Loenix34/3LbjY/
We also use change() to call associated events.
Instead of setting the value of the select, set the selected property of the given option, and then trigger a change.
$("option[value='the-value']").prop('selected', true);
$("#form select:first").change();
Fiddle Demo
I have an HTML select element (combo box) in a form with a couple if options in it. As always each option has a specific value. During the page's initialization I get a value from the server that I must use as the selected option in the list. Sometimes however I get a value from the server that does not match any of the available options. (grrrr)
I want to let the user know that the value I got from the server is not a valid option. What I'd like to do is show an empty select box (as if no selection was made) without having an actual empty option as one of the options. Also I'd like to use the default select element if possible. Is something like this possible?
Edit: When you set the value for a combox to '' in IE9 (I used $('select').val('') ) it empties the text in the combo box which is exactly what I need. Unfortunately only IE9 does this, so this is not an option.
Out of the box, HTML selects do not provide such functionality. You will have to add an empty <option value="somethingwrong">Please, pick a value</option> element and use scripting to check if the user has selected this specific value. I'd suggest catching both onchange event of the dropdown and onsubmit event of whole form.
You can insert option in select, and remove that whenever it is changed to reduce problems with that options
Check the Demo here
I have tried many ways to select an option with jquery including .val("0") .attr('selected', 'selected') and .attr('selected', 'true') and the TexoTela plugin. They all seem to update the html but the browser still shows the last item selected by the user.
Try out this fiddle... Select the dropdown value 1 and then click the link.
Is there any way to actually update the displayed value?
You mean change which item is selected?
$('select').val('0');
There are other ways, but this is the basic, following your example.
jsfiddle
You can read up on the documenation for .val() here, and the snippet:
checks, or selects, all the radio
buttons, checkboxes, and select
options that match the set of values.
EDIT for jQuery Mobile
For jQuery Mobile you also need to refresh the select as mentioned in the mobile docs:
If you manipulate a select via
JavaScript, you must call the refresh
method on it to update the visual
styling.
You can do this by:
$('select').selectmenu("refresh",true);
You're setting the option value to zero.
Set the select not the option val
$('select').val('0');
and you'll probably want to use an #id instead just a 'select' selector.
Still use your .val("0") to set the value, and to update in the browser you have to use one of the following:
.trigger("liszt:updated");
or
.trigger("chosen:updated");
Just one will work, depending on how you create your select.
It's gonna be like:
$("#Your_select").val('0');
$("#Your_select").trigger("liszt:updated");