The drop-down value is getting selected and I can see the value being highlighted in the drop-down but the display is always showing the first value of the drop-down. I am dynamically selecting the drop-down value in the java script depending on the value returned from other fields. The desktop version works fine but having problem with the mobile version and found that we are using jquery mobile version which could be the cause for this. Any help on how to overcome this.
Have already used the following
document.getElementById(ID).value = value;
Have provided selected=true, used the text option as well.
If i write a automation script my tests are getting passed as the value is getting selected properly but is not getting displayed in the dropdown. If I manually select the values from the drop down I can see the selected value being displayed in the drop-down.
To correctly assign value to a select widget, you need to marked its option to be selected:
document.getElementById('ID').getElementsByTagName('option')[1].selected = 'selected'
using the jquery text() method to assign the dynamic value to drop-down. use the below code
$("#dropDownID option:selected").text()
Related
I am trying to pull the text from the drop-down menu selection. If I use
document.getElementById("c6bf8c48-f2d9-eb11-bacb-00224804ce17").value
I get the numerical value of the selection. How I can get a text value?
Try document.getElementById("c6bf8c48-f2d9-eb11-bacb-00224804ce17").innerHTML
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.
How to keep selected value in a dropdown
my dropdown id is "devicePlatform_id" its values are Android,Windows,Smart Phone
while updation I want selected value chosen by user during saving for eg Android
so how can I do that in Jquery.I have broght data from db just wanted to keep the value
So what will be the code for that
$('#devicePublicationStatus_id'). ??
Use the .val() function:
$('#devicePublicationStatus_id').val("Android");
EDIT: I presumed that for "dropdown" you mean a "select" html element with "option" elements as childs.
EDIT2: As the other answer say i have not understand well if you want GET or SET the value in the dropdown. Use .val("Android") for set the current value, and only .val() for get the current value.
If I got your question right:
$('#devicePublicationStatus_id option:selected').val()
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");