This question already has answers here:
Javascript/jQuery: Set Values (Selection) in a multiple Select
(9 answers)
Closed 3 years ago.
I want to preselect of id (195,197,120) by code on load
in multiselect box
coresponding id 195,197,120 will preselected
<select class="width100" id="dialog_settings_event_selected_recipient" style="height:80px;" multiple="multiple">
<option value="120">Aaban bilal</option>
<option value="197">Aabid iqbal</option>
<option value="196">Aadan parvaiz shah</option>
<option value="262">Aadil bashir</option>
<option value="195">Aaksa iqbol</option>
<option value="233">Aamir shabir</option>
<option value="231">Aasma zahoor</option>
<option value="198">Aathir shah bhat</option>
<option value="208">Aatif dar</option>
<option value="244">Aatif manzoor</option>
<option value="77">Abrar Mushtaq</option>
<option value="68">Adnan Javid</option>
<option value="236">Adnan amin</option>
<option value="132">Adnan iqbal</option>
<option value="112">Adnan mushtaq pir</option>
<option value="252">Adnan nazir-107</option>
<option value="157">Afhan Mudasir</option>
<option value="154">Ahsan Yousuf</option>
<option value="87">Aijaz Ah Malik</option>
</select>
Try this to set values for multiple select
var data="120,197,195";
//Make an array
var dataarray=data.split(",");
// Set the value
$("#dialog_settings_event_selected_recipient").val(dataarray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="width100" id="dialog_settings_event_selected_recipient" style="height:80px;" multiple="multiple">
<option value="120">Aaban bilal</option>
<option value="197">Aabid iqbal</option>
<option value="196">Aadan parvaiz shah</option>
<option value="262">Aadil bashir</option>
<option value="195">Aaksa iqbol</option>
<option value="233">Aamir shabir</option>
<option value="231">Aasma zahoor</option>
<option value="198">Aathir shah bhat</option>
<option value="208">Aatif dar</option>
<option value="244">Aatif manzoor</option>
<option value="77">Abrar Mushtaq</option>
<option value="68">Adnan Javid</option>
<option value="236">Adnan amin</option>
<option value="132">Adnan iqbal</option>
<option value="112">Adnan mushtaq pir</option>
<option value="252">Adnan nazir-107</option>
<option value="157">Afhan Mudasir</option>
<option value="154">Ahsan Yousuf</option>
<option value="87">Aijaz Ah Malik</option>
</select>
Related
I have this scenario where i have 4 drop down boxes, where you can choose CM for a carport and a shed, within that carport.
I want to prompt the user with messages/errors in these scenarios:
the shed is chosen to be wider than it is long.
the shed is atleast 60cm smaller than the carports in both width and length
only one of the sheds dimensions being chosen
the form looks like this
<form name="createorder" action="FrontController" method="POST">
<input type="hidden" name="command" value="createorder">
<br>
Length of shed:<br>
<select name="lengthShed">
<option value="0">I do not want a shed</option>
<option value="180">180cm</option>
<option value="210">210cm</option>
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
</select>
<br>
Width of shed:<br>
<select name="widthShed">
<option value="0">I do not want a shed</option>
<option value="18=">180cm</option>
<option value="210">210cm</option>
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
</select>
<br>
Width:<br>
<select name="width">
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
<option value="750">750cm</option>
</select>
<br>
Length:<br>
<select name="length">
<option value="240">240cm</option>
<option value="270">270cm</option>
<option value="300">300cm</option>
<option value="330">330cm</option>
<option value="360">360cm</option>
<option value="390">390cm</option>
<option value="420">420cm</option>
<option value="450">450cm</option>
<option value="480">480cm</option>
<option value="510">510cm</option>
<option value="540">540cm</option>
<option value="570">570cm</option>
<option value="600">600cm</option>
<option value="630">630cm</option>
<option value="660">660cm</option>
<option value="690">690cm</option>
<option value="720">720cm</option>
<option value="750">750cm</option>
<option value="780">780cm</option>
</select>
Bind a function to your <select> elements onchange property.
let selects = document.querySelectorAll('select');
for(var i = 0; i < selects.length; i++){
selects[i].onchange = function(){
// Check for your conditions
// If your warning conditions are met, prompt user
}
}
That is, assuming you want the validation to happen when the user changes one of the select's values.
If you want to validate with a button click, you can instead bind the function to the click event of the button.
let button = document.getElementById(buttonID);
button.onclick = function(){
// Check for your conditions
// If conditions are met, prompt user
}
To prompt the user, you can use a simple alert() to which you pass your message. Or make a more elaborate function to do something custom.
As for your conditions, that sounds like commercial math. Might have to split your question in multiple questions here as there are a lot of possible awnsers.
It might be easier for you to add IDs to your selects so you can get them via document.getElementById(idString). You can then get their value through document.getElementById(idString).value. And to use it in math solving, you will need to parse the string that the <select> will return as value, like so parseInt(document.getelementById(idString).value).
I am using PHP to show years between 1940 and the current year and using a foreach loop to render each option inside a select element. Everything works as planned until I add an id to the select element that a block of jquery looks at for changes.
Essentially I am trying to console.log (for now) the value of whatever option gets selected but something about my jQuery/PHP is stopping the select from displaying the selected option. The console.log works as expected but the selected option doesn't update in the element. It always goes back to displaying the default option aka 2018 since it's the current year.
If I change the PHP foreach select/option to a static dropdown of plain HTML it works fine. Likewise, if I disconnect the jQuery but keep the PHP dynamically generated option list it works fine. Something about the combo of my PHP+jQuery is preventing the select from updating and I cannot figure what.
Here is my HTML/PHP code:
<select name="birthyear" id="birthyear">
<?php
// $years is setting the range from current year to 1940
$years = range(date('Y'), 1940);
foreach ($years as $year) {
echo "<option id='$year' value='$year'>".$year."</option>";
}
?>
</select>
Here is my jQuery:
$('#birthyear').on('change', function() {
var date = new Date();
var year = date.getFullYear();
var yearsOld = year - $(this).val();
console.log(yearsOld + " years old.");
if (yearsOld >= 18) {
console.log("age >= 18")
} else {
console.log("You aren't old enough!");
}
});
Am I missing something here?
Your code works fine... All I did was execute the PHP to get the HTML and then plugged it into JSFiddle.
I'd assume your problem is that you didn't properly include jQuery.
<select name="birthyear" id="birthyear">
<option id='2018' value='2018'>2018</option>
<option id='2017' value='2017'>2017</option>
<option id='2016' value='2016'>2016</option>
<option id='2015' value='2015'>2015</option>
<option id='2014' value='2014'>2014</option>
<option id='2013' value='2013'>2013</option>
<option id='2012' value='2012'>2012</option>
<option id='2011' value='2011'>2011</option>
<option id='2010' value='2010'>2010</option>
<option id='2009' value='2009'>2009</option>
<option id='2008' value='2008'>2008</option>
<option id='2007' value='2007'>2007</option>
<option id='2006' value='2006'>2006</option>
<option id='2005' value='2005'>2005</option>
<option id='2004' value='2004'>2004</option>
<option id='2003' value='2003'>2003</option>
<option id='2002' value='2002'>2002</option>
<option id='2001' value='2001'>2001</option>
<option id='2000' value='2000'>2000</option>
<option id='1999' value='1999'>1999</option>
<option id='1998' value='1998'>1998</option>
<option id='1997' value='1997'>1997</option>
<option id='1996' value='1996'>1996</option>
<option id='1995' value='1995'>1995</option>
<option id='1994' value='1994'>1994</option>
<option id='1993' value='1993'>1993</option>
<option id='1992' value='1992'>1992</option>
<option id='1991' value='1991'>1991</option>
<option id='1990' value='1990'>1990</option>
<option id='1989' value='1989'>1989</option>
<option id='1988' value='1988'>1988</option>
<option id='1987' value='1987'>1987</option>
<option id='1986' value='1986'>1986</option>
<option id='1985' value='1985'>1985</option>
<option id='1984' value='1984'>1984</option>
<option id='1983' value='1983'>1983</option>
<option id='1982' value='1982'>1982</option>
<option id='1981' value='1981'>1981</option>
<option id='1980' value='1980'>1980</option>
<option id='1979' value='1979'>1979</option>
<option id='1978' value='1978'>1978</option>
<option id='1977' value='1977'>1977</option>
<option id='1976' value='1976'>1976</option>
<option id='1975' value='1975'>1975</option>
<option id='1974' value='1974'>1974</option>
<option id='1973' value='1973'>1973</option>
<option id='1972' value='1972'>1972</option>
<option id='1971' value='1971'>1971</option>
<option id='1970' value='1970'>1970</option>
<option id='1969' value='1969'>1969</option>
<option id='1968' value='1968'>1968</option>
<option id='1967' value='1967'>1967</option>
<option id='1966' value='1966'>1966</option>
<option id='1965' value='1965'>1965</option>
<option id='1964' value='1964'>1964</option>
<option id='1963' value='1963'>1963</option>
<option id='1962' value='1962'>1962</option>
<option id='1961' value='1961'>1961</option>
<option id='1960' value='1960'>1960</option>
<option id='1959' value='1959'>1959</option>
<option id='1958' value='1958'>1958</option>
<option id='1957' value='1957'>1957</option>
<option id='1956' value='1956'>1956</option>
<option id='1955' value='1955'>1955</option>
<option id='1954' value='1954'>1954</option>
<option id='1953' value='1953'>1953</option>
<option id='1952' value='1952'>1952</option>
<option id='1951' value='1951'>1951</option>
<option id='1950' value='1950'>1950</option>
<option id='1949' value='1949'>1949</option>
<option id='1948' value='1948'>1948</option>
<option id='1947' value='1947'>1947</option>
<option id='1946' value='1946'>1946</option>
<option id='1945' value='1945'>1945</option>
<option id='1944' value='1944'>1944</option>
<option id='1943' value='1943'>1943</option>
<option id='1942' value='1942'>1942</option>
<option id='1941' value='1941'>1941</option>
<option id='1940' value='1940'>1940</option>
</select>
https://jsfiddle.net/gpfh150g/
So I have a a razor list set up like so.
#Html.ListBoxFor(m => m.Customer, new SelectList(Model.Customer, "Id", "Name", Model.Customer[0]), new { #id = "CustomerList", #onchange = "CustomerListSelectionChanged(this.value); SelectionGrab(this.innerHTML);" })
and the script is then a simple thing that runs like this
function SelectionGrab(e) {
alert(e);
}
However when it runs the this.value I just get the value of the selected item, and when i use the this.innerHTML it grabs the entire HTML of the Listbox...when all i want is the single selected Variable...
<select id="CustomerList" multiple="multiple" name="Customer" onchange="CustomerListSelectionChanged(this.value); SelectionGrab(this.innerHTML);"><option value="3">Nationwide</option>
<option value="5">C</option>
<option value="11">S</option>
<option value="14">Public Savings Credit Union (PSCU) </option>
<option value="15">Reliable Life Insurance</option>
<option value="18">Wellcare - "HIPPA CLIENT"</option>
<option value="21">Allianz</option>
<option value="26">World Omni - Southeast Toyota - Retail</option>
<option value="494">D</option>
<option value="497">Roanoke Gas (85115)</option>
<option value="498">World Omni-CenterOne (Retail)</option>
<option value="500">C</option>
<option value="502">Shenandoah Life - Individual (37243)</option>
<option value="614">POSTNET</option>
<option value="615">Test Account</option>
<option value="616">County of Chesterfield Taxes (26585,27144)</option>
<option value="617">County of Chesterfield Utilities (26725)</option>
<option value="619">Henrico County Utilities </option>
<option value="620">Henrico Personal Property (877-3369)</option>
<option value="621">Henrico Real Estate (321-3370)</option>
<option value="625">Time Warner Cable - Charlotte</option>
<option value="626">World Omni-Southeast Toyota (89)</option>
<option value="628">Home Furnishings - The Dump</option>
<option value="645">City of Chesapeake (1606-1375)</option>
<option value="848">MIXED</option>
<option value="856">CAPITAL ONE BNK (70884, 71083,70886)</option>
<option value="886">Vingina Natural Gas</option>
<option value="892">World Omni-Center One </option>
<option value="893">Reliable Life Insurance</option>
<option value="899">Liberty Life Insurance</option>
<option value="901">World Omni 70832 (88)</option>
<option value="991"></option>
<option value="998">Kemper</option>
<option value="1231">PNC Fleetcor</option>
<option value="1947">Fleetcor BP</option>
<option value="2069">FDR ARCO</option>
<option value="2102">NEBCO</option>
<option value="2252">Prince William County Service Authority</option>
<option value="2520">Guilford County Tax</option>
<option value="2543">FAIRFAX WATER</option>
<option value="2544">PSCU</option>
<option value="3195">SOLSTAS LAB/SPECTRUM LAB- "HIPPA CLIENT"</option>
<option value="3259"></option>
<option value="3260"></option>
<option value="3261"></option>
<option value="3262"></option>
<option value="3263"></option>
<option value="3264"></option>
<option value="3265"></option>
<option value="3266"></option>
<option value="3267"></option>
<option value="3268"></option>
<option value="3269"></option>
<option value="3270"></option>
<option value="3276"></option>
<option value="3292"></option>
<option value="3293"></option>
<option value="3431">Henrico Utility</option>
<option value="3437">CONA - CAP ONE NEW ACCOUNTS</option>
<option value="3480">Morris Broadband LLC</option>
<option value="3506">CAROLINA WEST WIRELESS</option>
<option value="3598"></option>
<option value="3616"></option>
<option value="3617"></option>
<option value="3620"></option>
<option value="3621"></option>
<option value="3622"></option>
<option value="3623"></option>
<option value="3624"></option>
<option value="3625"></option>
<option value="3626"></option>
<option value="3627"></option>
<option value="3628"></option>
<option value="3682"></option>
<option value="3683"></option>
<option value="3691"></option>
<option value="3692">TOWN OF CARY</option>
<option value="3705"></option>
<option value="3725">VANDERBILT/CLAYTON HOMES</option>
<option value="3731"></option>
<option value="3761">UHS GREENVILLE - "HIPPA CLIENT"</option>
<option value="3783"></option>
<option value="3785">City of Raleigh</option>
<option value="3786"></option>
<option value="3787"></option>
<option value="3793"></option>
<option value="3830"></option>
<option value="3831"></option>
<option value="3832"></option>
<option value="3837"></option>
<option value="3840"></option>
<option value="3841"></option>
<option value="3849">Fairwinds Credit Union</option>
<option value="3958"></option>
<option value="4084"></option>
<option value="4091"></option>
<option value="4107"></option>
<option value="4139"></option>
<option value="4145">Hampton Rd. Sanitation</option>
<option value="4156"></option>
<option value="4167">G E Debt Cancellation</option>
<option value="4171"></option>
<option value="4200"></option>
<option value="4203">Dearborn National Life Insurance</option>
<option value="4206"></option>
<option value="4212">Athene Annuity</option>
<option value="4216"></option>
<option value="4217"></option>
<option value="4229"></option>
<option value="4242"></option>
<option value="4253">USHA-Knology</option>
<option value="4254">Comporium-Knology</option>
<option value="4256"></option>
<option value="4274">CRS 300 (71104, 71106, 71107)</option>
<option value="4275">Metris 303</option>
<option value="4285"></option>
<option value="4286"></option>
<option value="4287"></option>
<option value="4288"></option>
<option value="4289"></option>
<option value="4291"></option>
<option value="4313">Volvo Retail</option>
<option value="4314">Volvo- Lease</option>
<option value="4346">Capital One Restrictive Wording </option>
<option value="4494"></option>
<option value="4499"></option>
<option value="4580"></option>
<option value="4602"></option>
<option value="4603"></option>
<option value="4604"></option>
<option value="4605"></option>
<option value="4606"></option>
<option value="4607"></option>
<option value="4608"></option>
<option value="4610"></option>
<option value="4611"></option>
<option value="4612"></option>
<option value="4618"></option>
<option value="4622"></option>
<option value="4623"></option>
<option value="4629"></option>
<option value="4630"></option>
<option value="4631"></option>
<option value="4632"></option>
<option value="4633"></option>
<option value="4637"></option>
<option value="4662">Carolina Health Care - 'HIPPA CLIENT"</option>
<option value="4664">City of Richmond RE (043)</option>
<option value="4674">City of Richmond PP (042) </option>
<option value="4685">U</option>
</select>
The above is what the html list box is in view. Now...what I want is when i select let's say the "C"
I don't want the entire CustomerList's innerHTML...just the selected's innerHTML... Help please?
In SelectionGrab function call, pass the reference of the element by using this:
SelectionGrab(this)
and then in function definition extract the selected value this way:
function SelectionGrab(e) {
var selectedCustomerId = e.options[e.selectedIndex].innerHTML;
alert(selectedCustomerId);
}
this.options[this.selectedIndex].innerHTML
Here is my HTMl select menu:
<select name="shipping_city" id="shipping-city-select" title="">
<option value="">Изберете град</option>
<option city_en="Aytos" value="1">гр. Айтос</option>
<option city_en="Burgas" value="2">гр. Бургас</option>
<option city_en="Asenovgrad" value="3">гр. Асеновград</option>
<option city_en="Blagoevgrad" value="4">гр. Благоевград</option>
<option city_en="Botevgrad" value="5">гр. Ботевград</option>
<option city_en="Byala Rusensko" value="6">гр. Бяла, Русенско</option>
<option city_en="Varna" value="7">гр. Варна</option>
<option city_en="Veliko Tyrnovo" value="8">гр. Велико Търново</option>
<option city_en="Vidin" value="9">гр. Видин</option>
<option city_en="Vratsa" value="10">гр. Враца</option>
<option city_en="Gabrovo" value="11">гр. Габрово</option>
<option city_en="Gorna Oryahovitsa" value="12">гр. Горна Оряховица</option>
<option city_en="Devnya" value="13">гр. Девня</option>
<option city_en="Dryanovo" value="14">гр. Дряново</option>
<option city_en="Dobrich" value="15">гр. Добрич</option>
<option city_en="Dupnitsa" value="16">гр. Дупница</option>
<option city_en="Ihtiman" value="17">гр. Ихтиман</option>
<option city_en="Kazanlyk" value="18">гр. Казанлък</option>
<option city_en="Karlovo" value="19">гр. Карлово</option>
<option city_en="Karnobat" value="20">гр. Карнобат</option>
<option city_en="Kostenets" value="21">гр. Костенец</option>
<option city_en="Kyustendil" value="22">гр. Кюстендил</option>
<option city_en="Kyrdzhali" value="23">гр. Кърджали</option>
<option city_en="Lovech" value="24">гр. Ловеч</option>
<option city_en="Lom" value="25">гр. Лом</option>
<option city_en="Montana" value="26">гр. Монтана</option>
<option city_en="Nova Zagora" value="27">гр. Нова Загора</option>
<option city_en="Pazardzhik" value="29">гр. Пазарджик</option>
<option city_en="Pernik" value="30">гр. Перник</option>
<option city_en="Petrich" value="31">гр. Петрич</option>
<option city_en="Pleven" value="32">гр. Плевен</option>
<option city_en="Plovdiv" value="33">гр. Пловдив</option>
<option city_en="Razgrad" value="34">гр. Разград</option>
<option city_en="Ruse" value="35">гр. Русе</option>
<option city_en="Sandanski" value="36">гр. Сандански</option>
<option city_en="Sevlievo" value="38">гр. Севлиево</option>
<option city_en="Sliven" value="39">гр. Сливен</option>
<option city_en="Sopot" value="40">гр. Сопот</option>
<option city_en="Sofiya" value="41">гр. София</option>
<option city_en="Stara Zagora" value="42">гр. Стара Загора</option>
<option city_en="Troyan" value="43">гр. Троян</option>
<option city_en="Tyrgovishte" value="44">гр. Търговище</option>
<option city_en="Haskovo" value="45">гр. Хасково</option>
<option city_en="Chirpan" value="46">гр. Чирпан</option>
<option city_en="Shumen" value="47">гр. Шумен</option>
<option city_en="Yambol" value="48">гр. Ямбол</option>
<option city_en="Beloslav" value="50">гр. Белослав</option>
<option city_en="Breznik" value="52">гр. Брезник</option>
<option city_en="Velingrad" value="53">гр. Велинград</option>
<option city_en="Elin Pelin" value="55">гр. Елин Пелин</option>
<option city_en="Etropole" value="56">гр. Етрополе</option>
<option city_en="Kalofer" value="59">гр. Калофер</option>
<option city_en="Kozloduy" value="60">гр. Козлодуй</option>
<option city_en="Lyaskovets" value="61">гр. Лясковец</option>
<option city_en="Marten" value="62">гр. Мартен</option>
<option city_en="Mezdra" value="63">гр. Мездра</option>
<option city_en="Panagyurishte" value="64">гр. Панагюрище</option>
<option city_en="Peshtera" value="65">гр. Пещера</option>
<option city_en="Pomorie" value="67">гр. Поморие</option>
<option city_en="Pravets" value="68">гр. Правец</option>
<option city_en="Provadiya" value="69">гр. Провадия</option>
<option city_en="Radomir" value="70">гр. Радомир</option>
<option city_en="Svilengrad" value="72">гр. Свиленград</option>
<option city_en="Svishtov" value="73">гр. Свищов</option>
<option city_en="Senovo" value="74">гр. Сеново</option>
<option city_en="Silistra" value="75">гр. Силистра</option>
<option city_en="Smolyan" value="76">гр. Смолян</option>
<option city_en="Tryavna" value="78">гр. Трявна</option>
<option city_en="Apriltsi" value="82">гр. Априлци</option>
<option city_en="Ardino" value="83">гр. Ардино</option>
<option city_en="Balchik" value="86">гр. Балчик</option>
<option city_en="Bankya" value="87">гр. Банкя</option>
<option city_en="Bansko" value="88">гр. Банско</option>
<option city_en="Batak" value="90">гр. Батак</option>
<option city_en="Belene" value="91">гр. Белене</option>
<option city_en="Belovo" value="93">гр. Белово</option>
<option city_en="Bobov Dol" value="95">гр. Бобов Дол</option>
<option city_en="Brezovo" value="102">гр. Брезово</option>
<option city_en="Perushtitsa" value="103">гр. Перущица</option>
<option city_en="Syedinenie" value="104">гр. Съединение</option>
<option city_en="Rakovski" value="105">гр. Раковски</option>
<option city_en="Pyrvomay" value="106">гр. Първомай</option>
<option city_en="Sadovo" value="107">гр. Садово</option>
<option city_en="Byala Slatina" value="108">гр. Бяла Слатина</option>
<option city_en="Knezha" value="109">гр. Кнежа</option>
<option city_en="Roman" value="110">гр. Роман</option>
<option city_en="Septemvri" value="113">гр. Септември</option>
<option city_en="Stamboliyski" value="114">гр. Стамболийски</option>
<option city_en="Strelcha" value="115">гр. Стрелча</option>
<option city_en="Rakitovo" value="116">гр. Ракитово</option>
<option city_en="Rila" value="117">гр. Рила</option>
<option city_en="Bratsigovo" value="118">гр. Брацигово</option>
<option city_en="Tsarevo" value="119">гр. Царево</option>
<option city_en="Sozopol" value="121">гр. Созопол</option>
<option city_en="Slynchev Bryag" value="122">гр. Слънчев Бряг</option>
<option city_en="Primorsko" value="124">гр. Приморско</option>
<option city_en="Nesebyr" value="125">гр. Несебър</option>
<option city_en="Kableshkovo" value="127">гр. Каблешково</option>
<option city_en="Zavet" value="128">гр. Завет</option>
<option city_en="Popovo" value="129">гр. Попово</option>
<option city_en="Kubrat" value="130">гр. Кубрат</option>
<option city_en="Isperih" value="131">гр. Исперих</option>
<option city_en="Tsar Kaloyan" value="132">гр. Цар Калоян</option>
<option city_en="Hisarya" value="133">гр. Хисаря</option>
<option city_en="Klisura" value="134">гр. Клисура</option>
<option city_en="Kirkovo" value="136">гр. Кирково</option>
<option city_en="Krumovgrad" value="137">гр. Крумовград</option>
<option city_en="Momchilgrad" value="138">гр. Момчилград</option>
<option city_en="Vyrshets" value="139">гр. Вършец</option>
<option city_en="Boychinovtsi" value="140">гр. Бойчиновци</option>
<option city_en="Dolna Oryahovitsa" value="141">гр. Долна Оряховица</option>
<option city_en="Strazhitsa" value="142">гр. Стражица</option>
<option city_en="Elena" value="143">гр. Елена</option>
<option city_en="Radnevo" value="145">гр. Раднево</option>
<option city_en="Gylybovo" value="146">гр. Гълъбово</option>
<option city_en="Dimitrovgrad" value="147">гр. Димитровград</option>
<option city_en="Kresna" value="148">гр. Кресна</option>
<option city_en="Harmanli" value="150">гр. Харманли</option>
<option city_en="Lyubimets" value="151">гр. Любимец</option>
<option city_en="Bozhurishte" value="153">гр. Божурище</option>
<option city_en="Boboshevo" value="154">гр. Бобошево</option>
<option city_en="Brusartsi" value="157">гр. Брусарци</option>
<option city_en="Veliki Preslav" value="158">гр. Велики Преслав</option>
<option city_en="Vylchedrym" value="161">гр. Вълчедръм</option>
<option city_en="Vylchi Dol" value="162">гр. Вълчи Дол</option>
<option city_en="General Toshevo" value="163">гр. Генерал Тошево</option>
<option city_en="Gotse Delchev" value="168">гр. Гоце Делчев</option>
<option city_en="Gurkovo" value="169">гр. Гурково</option>
<option city_en="Debelets" value="171">гр. Дебелец</option>
<option city_en="Dolni Dybnik" value="174">гр. Долни Дъбник</option>
<option city_en="Dragoman" value="176">гр. Драгоман</option>
<option city_en="Zlataritsa" value="180">гр. Златарица</option>
<option city_en="Zlatograd" value="181">гр. Златоград</option>
<option city_en="Kavarna" value="183">гр. Каварна</option>
<option city_en="Kiten" value="188">гр. Китен</option>
<option city_en="Kostinbrod" value="191">гр. Костинброд</option>
<option city_en="Kotel" value="192">гр. Котел</option>
<option city_en="Kocherinovo" value="193">гр. Кочериново</option>
<option city_en="Kranevo" value="194">гр. Кранево</option>
<option city_en="Kula" value="198">гр. Кула</option>
<option city_en="Lukovit" value="201">гр. Луковит</option>
<option city_en="Madan" value="202">гр. Мадан</option>
<option city_en="Miziya" value="205">гр. Мизия</option>
<option city_en="Myglizh" value="208">гр. Мъглиж</option>
<option city_en="Nikopol" value="210">гр. Никопол</option>
<option city_en="Novi Iskyr" value="212">гр. Нови Искър</option>
<option city_en="Novi Pazar" value="213">гр. Нови Пазар</option>
<option city_en="Omurtag" value="214">гр. Омуртаг</option>
<option city_en="Oryahovo" value="215">гр. Оряхово</option>
<option city_en="Pavel Banya" value="216">гр. Павел Баня</option>
<option city_en="Pavlikeni" value="217">гр. Павликени</option>
<option city_en="Pliska" value="220">гр. Плиска</option>
<option city_en="Polski Trymbesh" value="222">гр. Полски Тръмбеш</option>
<option city_en="Rudozem" value="229">гр. Рудозем</option>
<option city_en="Samokov" value="230">гр. Самоков</option>
<option city_en="Sveti Vlas" value="231">гр. Свети Влас</option>
<option city_en="Svoge" value="233">гр. Своге</option>
<option city_en="Simeonovgrad" value="234">гр. Симеоновград</option>
<option city_en="Simitli" value="235">гр. Симитли</option>
<option city_en="Slivnitsa" value="237">гр. Сливница</option>
<option city_en="Sredets" value="240">гр. Средец</option>
<option city_en="Suhindol" value="243">гр. Сухиндол</option>
<option city_en="Tvyrditsa" value="244">гр. Твърдица</option>
<option city_en="Tervel" value="245">гр. Тервел</option>
<option city_en="Teteven" value="246">гр. Тетевен</option>
<option city_en="Tutrakan" value="248">гр. Тутракан</option>
<option city_en="Cherven Bryag" value="249">гр. Червен Бряг</option>
<option city_en="Chepelare" value="250">гр. Чепеларе</option>
<option city_en="Shivachevo" value="252">гр. Шивачево</option>
<option city_en="Shipka" value="253">гр. Шипка</option>
<option city_en="Yablanitsa" value="254">гр. Ябланица</option>
<option city_en="Dulovo" value="256">гр. Дулово</option>
<option city_en="Devin" value="257">гр. Девин</option>
<option city_en="Tryn" value="260">гр. Трън</option>
<option city_en="Elhovo" value="263">гр. Елхово</option>
<option city_en="Straldzha" value="276">гр. Стралджа</option>
<option city_en="Pirdop" value="277">гр. Пирдоп</option>
<option city_en="Levski" value="281">гр. Левски</option>
<option city_en="Zlatitsa" value="286">гр. Златица</option>
<option city_en="Belogradchik" value="288">гр. Белоградчик</option>
<option city_en="Dolna Banya" value="294">гр. Долна Баня</option>
<option city_en="Dospat" value="295">гр. Доспат</option>
<option city_en="Krivodol" value="297">гр. Криводол</option>
<option city_en="Alfatar" value="299">гр. Алфатар</option>
<option city_en="Bulgaria" value="1033"> България</option>
<option city_en="Borovo" value="1238">гр. Борово</option>
<option city_en="Lyaskovets Sklad" value="1240">гр. Лясковец Склад</option>
<option city_en="Loznitsa" value="1241">гр. Лозница</option>
<option city_en="Kaspichan" value="1245">гр. Каспичан</option>
<option city_en="Hadzhidimovo" value="1253">гр. Хаджидимово</option>
<option city_en="Razlog" value="1270">гр. Разлог</option>
<option city_en="Dobrinishte" value="1272">гр. Добринище</option>
<option city_en="Belitsa" value="1277">гр. Белица</option>
<option city_en="Yakoruda" value="1278">гр. Якоруда</option>
<option city_en="Aksakovo" value="1284">гр. Аксаково</option>
<option city_en="Shabla" value="1290">гр. Шабла</option>
<option city_en="Obzor" value="1301">гр. Обзор</option>
<option city_en="Dve Mogili" value="1382">гр. Две Могили</option>
<option city_en="Banya" value="22454">гр. Баня</option>
<option city_en="Bolyarovo" value="22668">гр. Болярово</option>
<option city_en="Bregovo" value="22756">гр. Брегово</option>
<option city_en="Bylgarovo" value="22859">гр. Българово</option>
<option city_en="Byala Cherkva" value="22895">гр. Бяла Черква</option>
<option city_en="Vetren" value="22976">гр. Ветрен</option>
<option city_en="Vyrbitsa" value="23147">гр. Върбица</option>
<option city_en="Glavinitsa" value="23260">гр. Главиница</option>
<option city_en="Godech" value="23281">гр. Годеч</option>
<option city_en="Gramada" value="23481">гр. Грамада</option>
<option city_en="Gulyantsi" value="23516">гр. Гулянци</option>
<option city_en="Dimovo" value="23631">гр. Димово</option>
<option city_en="Dolna Mitropoliya" value="23735">гр. Долна Митрополия</option>
<option city_en="Dunavtsi" value="23895">гр. Дунавци</option>
<option city_en="Dylgopol" value="23942">гр. Дългопол</option>
<option city_en="Zhergovets" value="24037">гр. Жерговец</option>
<option city_en="Zemen" value="24138">гр. Земен</option>
<option city_en="Iskyr" value="24274">гр. Искър</option>
<option city_en="Kameno" value="24356">гр. Камено</option>
<option city_en="Kaolinovo" value="24373">гр. Каолиново</option>
<option city_en="Kermen" value="24432">гр. Кермен</option>
<option city_en="Koynare" value="24528">гр. Койнаре</option>
<option city_en="Koprivshtitsa" value="24584">гр. Копривщица</option>
<option city_en="Kostandovo" value="24609">гр. Костандово</option>
<option city_en="Kuklen" value="24745">гр. Куклен</option>
<option city_en="Letnitsa" value="24837">гр. Летница</option>
<option city_en="Lyki" value="24919">гр. Лъки</option>
<option city_en="Madara" value="24960">гр. Мадара</option>
<option city_en="Merichleri" value="25104">гр. Меричлери</option>
<option city_en="Momin Prohod" value="25198">гр. Момин Проход</option>
<option city_en="Nedelino" value="25286">гр. Неделино</option>
<option city_en="Nikolaevo" value="25315">гр. Николаево</option>
<option city_en="Opaka" value="25421">гр. Опака</option>
<option city_en="Pordim" value="25744">гр. Пордим</option>
<option city_en="Sapareva Banya" value="26144">гр. Сапарева Баня</option>
<option city_en="Slivo Pole" value="26321">гр. Сливо Поле</option>
<option city_en="Suvorovo" value="26562">гр. Суворово</option>
<option city_en="Sungurlare" value="26566">гр. Сунгурларе</option>
<option city_en="Syrnitsa" value="26600">гр. Сърница</option>
<option city_en="Topolovgrad" value="26671">гр. Тополовград</option>
<option city_en="Trystenik" value="26722">гр. Тръстеник</option>
<option city_en="Ugyrchin" value="26770">гр. Угърчин</option>
<option city_en="Tsarev Brod" value="26866">гр. Царев Брод</option>
<option city_en="Chiprovtsi" value="27038">гр. Чипровци</option>
<option city_en="Shtryklevo" value="27120">гр. Щръклево</option>
<option city_en="Dolno Shivachevo" value="27229">гр. Долно Шивачево</option>
<option city_en="Krayna" value="27312">гр. Крайна</option>
<option city_en="Krichim" value="27338">гр. Кричим</option>
<option city_en="Berkovitsa" value="27399">гр. Берковица</option>
<option city_en="Panichishte" value="27465">гр. Паничище</option>
<option city_en="AEC Kozloduj" value="27493"> АЕЦ Козлодуй</option>
<option city_en="Lukoil Neftohim" value="27500">гр. Лукойл Нефтохим</option>
<option city_en="Mobilen RC" value="27543">гр. Мобилен РЦ</option>
<option city_en="izvoz" value="55950">гр. Извоз</option>
<option city_en="Fürth" value="61003">гр. Фюрт</option>
<option city_en="Mobilen RC (Rujinci)" value="61233">гр. Мобилен РЦ (Ружинци)</option>
</select>
Here is what i find to be used for sorting options, but it is sorting them for value.
var options = $("#shipping-city-select option"); // Collect options
options.detach().sort(function(a,b) { // Detach from select, then Sort
var at = $(a).text();
var bt = $(b).text();
return (at > bt)?1:((at < bt)?-1:0); // Tell the sort function how to order
});
options.appendTo("#shipping-city-select"); // Re-attach to select
How can i REsort all the options in this select menu by city_en attribute alphabetically using jQuery ?
Thanks in advance!
Firstly, your HTML is invalid. You cannot invent your own attributes. If you want to store custom data with an element, use a data-* attribute, like this:
<option data-city-en="Aytos" value="1">гр. Айтос</option>
From there you can amend your sort logic to read that data attribute:
$("#shipping-city-select option[data-city-en]").sort(function(a, b) {
var at = $(a).data('city-en');
var bt = $(b).data('city-en');
return at > bt ? 1 : (at < bt) ? -1 : 0;
}).appendTo("#shipping-city-select");
Example fiddle
Note that you don't need to detach() the elements before you append() them in the corrected order.
Also note that you can sort directly on the text containing the Cyrillic characters if you use localeCompare():
$("#shipping-city-select option[data-city-en]").sort(function(a, b) {
return $(a).text().localeCompare($(b).text());
}).appendTo("#shipping-city-select");
Example fiddle
I have 2 drop downs in HTML both representing months. So I want a validation like following.
If I select the first drop down month as April, then the next drop-down menu should start from the month April. If the first one is changed to June then the second should change to June.
<div id="head2" style="width:15%;float:right;margin-left:5px;">
<select id='gMonth2' onchange="show_month()">
<option value=''>--Select Month--</option>
<option selected value='1'>Janaury</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
</div>
<div id="head1" style="width:15%;float:right;margin-left:5px;">
<select id='gMonth1'>
<option value=''>--Select Month--</option>
<option selected value='1'>Janaury</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
</div>
The function will be:
function show_month()
{
//
}
How should I write that function?
Easy with jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script>
$(function(){
$('#gMonth2').change(function(){
var month = $(this).val();
$('#gMonth1').val(month);
});
});
</script>
and skip the onChange event in the first select...
Working example here: http://jsfiddle.net/sCnEZ/1/
Your JS function could be like this:
function show_month(obj) {
document.getElementById('gMonth1').selectedIndex = obj.selectedIndex;
}
You should change onchange="show_month()" with onchange="show_month(this)"
Check out this jsfiddle