I am using following script but the problem is script
is not working properly.Some time work and some time show wrong text.Instead of selected text.it Show me full text like this.
SELECT MINOR علوم الحاسب الآلي هندسة الطيران والفضاء والتكنولوجيا
<select class="cslMinor_Title" id="slMinor_Title">
<option value="-1">Select Minor</option>
<option value="201">علوم الحاسب الآلي</option>
<option value="203">نظم المعلومات</option>
<option value="299">تكنولوجيا المعلومات الأخرى</option>
</select>
$('#slMinor_Title').on('change', function ()
{
alert($('.cslMinor_Title').find('option:selected').text());
});
This is happen when i submit FORM through update panel and i am using this option in dialog box.
I want selected text not like below
SELECT MINOR علوم الحاسب الآلي هندسة الطيران والفضاء والتكنولوجيا
and also i tried many options like below
jQuery("#slMinor_Title option:selected").text()
But all are giving same result.Guide me what is the problem with code.
See this
$('#dropDownId :selected').text();
Try val() function to get value.
jQuery("#slMinor_Title option:selected").val();
Mark it as answer if you think it is helpful for you.
Related
I have a select with some options and i want to avoid that the first option is the selected one, so i do the following and after that i initialize the select2:
<select class='form-control form-control-sm'>
<option value='100294'>Aguacate</option>
<option value='400172'>Bundle Editable</option>
<option value='100291'>Bundle no Editable</option>
<option value='100260'>Camara</option>
</select>
$('select').val([]);
$('select').select2({
tags:true
});
The problem is that, for some reason when i set the select2 tag options to true, after typing 2 characters, the first value is automatically selected and i don't know why
Fiddle: https://jsfiddle.net/wolfteam20/2pwkLq6j/1/
Any help would be appreciated
Edit: I think this one is a bug in select2
Issue
I know it's been a while, but there is a workaround for this.
Turns out that if you add an empty <option> to the select tag, and add a placeholder, the issue is fixed.
JSFiddle
I am attempting to retrieve and set the selected value of a select element (drop down list) with jQuery.
for retrievel i have tried $("#myId").find(':selected').val(), as well as $("#myId").val() but both return undefined.
Any insight into this problem would be much appreciated.
to get/set the actual selectedIndex property of the select element use:
$("#select-id").prop("selectedIndex");
$("#select-id").prop("selectedIndex",1);
The way you have it is correct at the moment. Either the id of the select is not what you say or you have some issues in the dom.
Check the Id of the element and also check your markup validates at here at W3c.
Without a valid dom jQuery cannot work correctly with the selectors.
If the id's are correct and your dom validates then the following applies:
To Read Select Option Value
$('#selectId').val();
To Set Select Option Value
$('#selectId').val('newValue');
To Read Selected Text
$('#selectId>option:selected').text();
$('#myId').val() should do it, failing that I would try:
$('#myId option:selected').val()
When setting with JQM, don't forget to update the UI:
$('#selectId').val('newValue').selectmenu('refresh', true);
$("#myId").val() should work if myid is the select element id!
This would set the selected item: $("#myId").val('VALUE');
Suppose you have created a Drop Down list using SELECT tag like as follows,
<select id="Country">
Now if you want to see what is the selected value from drop down using JQuery then, simply put following line to retrieve that value..
var result= $("#Country option:selected").text();
it will work fine.
I know this is old but I just had a bear of a time with Razor, could not get it to work no matter how hard I tried. Kept coming back as "undefined" no matter if I used "text" or "html" for attribute. Finally I added "data-value" attribute to the option and it read that just fine.
<option value="1" data-value="MyText">MyText</option>
var DisplayText = $(this).find("option:selected").attr("data-value");
$( "#myId option:selected" ).text(); will give you the text that you selected in the drop down element. either way you can change it to .val(); to get the value of it . check the below coding
<select id="myId">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>`
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
Try this
$('#your_select_element_id').val('your_value').attr().add('selected');
I am looking to duplicate the value of a select input into a second input field using Javascript. I tried to do this using an onchange event call. What should the js code be in order to populate the input field id ='sort' upon change in the select function?
Here is my code
<input id = "sort">
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
<option value="Ascending"> Ascending</option>
<option value="Descending"> Descending</option>
</select>
This does not work. I see the error lies in value assignment (('#sort_select').val();)What is the correct way to do this?
JS solution: FIDDLE
change this:
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
to
<select name="sort_select" onchange="document.getElementById('sort').value = this.value;">
Use this code
$("select").change(function(){
$("#sort").val($(this).val());
});
Fiddle
http://jsfiddle.net/5S7mh/
onChange="document.getElementById('sort').value = (this.options[this.selectedIndex]).value;"
Don't use that big ugly line as inline JS though.
Also, start with populating the input box.
Is it possible to disable an option after creating a msDropdown plugin?
I explain my problem better.
I want to put html into each option with icons, text and some other stuff, so first i create an empty select then I add each option with the add function:
dropdown.add({text:$price.html(), value:'normal', className:'normal'});
The problem is that if a certain condition happen I have to disable one option, but there are no way to set an option disabled by using plugin settings.
There is the possibility to make an option disabled only by setting the related parameter disabled=disabled into the select before to call the msDropdown function, but I can't use this solution since I have to put dinamically html into option text.
Is there another way to do it?
Thank you for your help.
I found a solution.
I create my select empty and I fill each option with add function as before, but when that condition happen just do this:
var dropdown = $('select[name="priceType"]').msDropdown().data("dd");
if(credits_error) { // option must be disabled
dropdown.destroy(); // Make it a simple select
$('select[name="priceType"] option').attr('disabled', 'disabled');
dropdown = $('select[name="priceType"]').msDropdown().data("dd");
}
This way first I make it a simple select by calling destroy function, then I set properly the disabled attribute and I create a new msDropdown select.
It works for me, I tested it on IE, FF and Chrome
Yes, it is possibile. It can be done by using the disabled property of the option tag:
<select id="payments" name="payments" style="width:250px;">
<option value="" data-description="Choos your payment gateway">Payment Gateway</option>
<option value="amex" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Amex-56.png" data-description="My life. My card...">Amex</option>
<option value="Discover" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Discover-56.png" data-description="It pays to Discover...">Discover</option>
<option value="Mastercard" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Mastercard-56.png" data-title="For everything else..." data-description="For everything else...">Mastercard</option>
<option value="cash" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Cash-56.png" data-description="Sorry not available..." disabled="true">Cash on devlivery</option>
<option value="Visa" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Visa-56.png" data-description="All you need...">Visa</option>
<option value="Paypal" data-image="http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Paypal-56.png" data-description="Pay and get paid...">Paypal</option>
</select>
The option 'cash' will be disabled.
Here is a working fiddle: http://jsfiddle.net/NKQRj/1/
EDIT
In this second example data are loaded from JSON data using:
$("#payments").msDropDown({byJson:{data:jsonData, name:'payments2'}}).data("dd");
to fill the elements.
You can define your options as disabled in your JSON data using disabled: true property:
{image:'http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/images/msdropdown/icons/Cash-56.png', description:'Sorry not available...', value:'cash', text:'Cash on devlivery', disabled:true},
here is a working fiddle: http://jsfiddle.net/NKQRj/3/
html code:
<select>
<br>
<option>1</option><br>
<option>2</option><br>
</select>
This select will default display the first option item(display 1).
Now i want to change select to display the second item by jquery when dom is ready, but i tried several times, all failed.The following is my attempt:
$('select').prop('selectIndex', 1);
$('option').eq(1).attr('selected', 'selected');
$('option').eq(1).prop('selected', true);
default set select's style to 'display:none' in html code, then try above three ways and finally invoke $('select').show()
Maybe, i am only setting the dom value, not tell browser to refresh 'select'.
Do you konw the other way to refresh default display option in select?
You have to add values to your options from select.
<select>
<option value="1">First</option>
<option value="2">Second</option>
</select>
Then, just set the value "2".
$("select").val("2");
Or, you can do this simply setting the second value from select.
$("select").val(2);
See the working example here.
This is enough
$("select").val(2);
i think you want to select option 2 when page is load.
<select>
<option>1</option>
<option selected="selected">2</option>
</select>