keep selected value in dropdown in Jquery - javascript

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()

Related

Select2 Widge Reset Selected Value

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.

Dynamically assigned value in dropdown always displaying the default value

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()

show empty value in html select element

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

How to change rendered selected option in select box with jquery

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");

Display new field when choose specific value (JS/HTML)

I have this form:
http://dl.dropbox.com/u/14698783/project/register/form.htm
The city input is hidden by default.
I want to display it when the visitor choose USA or Canada only.
For this, I'm using jQuery. First, I set up and event handler for the country dropdown using the change method - this will be raised when the value is changed (not surprisingly). Then test if the value selected is in a set of accepted values (for this, I'm using the 'in' operator). Since this is an input element, I can just reference element.value without wrapping it in jQuery. Finally, use the toggle, passing in the boolean value to indicate if the select should be shown or hidden.
var valuesToShowFor = [0, 1]; // USA + Canada
$("#title").change(function() {
var shouldShowCity = (this.value in valuesToShowFor);
$("#city").toggle(shouldShowCity);
});
Example: http://jsfiddle.net/jonathon/xULyc/
For this, I recommend having IDs on your elements. It makes it much easier and neater to do the jQuery selects - otherwise you'd need something like $("input[name='title']") to reference the element.
Here are some clues for you:
Catch onchange with jQuery .change()
Get the selected option value with jQuery .val()
If the selection option value is one of those you need, use jQuery .show() to show the other input box (and .hide() to hide it)

Categories

Resources