Reputation: 57
I have a drop-down in asp.net core with country name and code. i need, when i select a drop-down value than it should be set a only code in drop-down. I have a screenshot you can see that,
now i have a second screen shot i want to like this drop-down after select value. second Screen Shot
my view drop-down code is
<select class="form-control" id="countryDropdown" >
@foreach (var country in Model)
{
<option value="@country.id"
data-code="@country.codeName">
@country.Name
</option>
}
</select>
html dropdown is
<select class="form-control" id="countryDropdown">
<option value="3" data-code="93">
Afghanistan (93)
</option>
<option value="264" data-code="">
Åland Islands ()
</option>
<option value="4" data-code="355">
Albania (355)
</option>
<option value="5" data-code="213">
Algeria (213)
</option>
<option value="6" data-code="1">
American Samoa (1)
</option>
<option value="7" data-code="376">
Andorra (376)
</option>
<option value="8" data-code="244">
Angola (244)
</option>
<option value="9" data-code="1">
Anguilla (1)
</option>
<option value="266" data-code="">
Anonymous Proxy ()
</option>
<option value="10" data-code="672">
Antarctica (672)
</option>
<option value="11" data-code="1">
Antigua and Barbuda (1)
</option>
<option value="12" data-code="54">
Argentina (54)
</option>
<option value="13" data-code="374">
Armenia (374)
</option>
<option value="14" data-code="297">
Aruba (297)
</option>
<option value="268" data-code="">
Asia/Pacific Region ()
</option>
</select>
Upvotes: 1
Views: 967
Reputation: 1895
can you please try this.
This very simple and easy to understand and faster.
Please check this link JsFiddle
Note: please uncomment the console.log for more detail.
and You can modify this code as per your requirement here I'm sowing the current data-code display in the dropdown option.
$(document).ready(function() {
$("#countryDropdown option:first").text($("#countryDropdown option:first").data("code"));
$("#countryDropdown").change(function(event)
{
var oCurrentOption =this.options[event.target.selectedIndex];
$("#countryDropdown option[value="+oCurrentOption.value+"]").text($(oCurrentOption).data("code"));
//console.log(oCurrentOption);
//console.log("Data Code "+$(oCurrentOption).data("code"));
//console.log("Current Text "+oCurrentOption.text);
//console.log("Current Value "+oCurrentOption.value);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="countryDropdown">
<option value="3" data-code="93">
Afghanistan (93)
</option>
<option value="264" data-code="">
Åland Islands ()
</option>
<option value="4" data-code="355">
Albania (355)
</option>
<option value="5" data-code="213">
Algeria (213)
</option>
<option value="6" data-code="1">
American Samoa (1)
</option>
<option value="7" data-code="376">
Andorra (376)
</option>
<option value="8" data-code="244">
Angola (244)
</option>
<option value="9" data-code="1">
Anguilla (1)
</option>
<option value="266" data-code="">
Anonymous Proxy ()
</option>
<option value="10" data-code="672">
Antarctica (672)
</option>
<option value="11" data-code="1">
Antigua and Barbuda (1)
</option>
<option value="12" data-code="54">
Argentina (54)
</option>
<option value="13" data-code="374">
Armenia (374)
</option>
<option value="14" data-code="297">
Aruba (297)
</option>
<option value="268" data-code="">
Asia/Pacific Region ()
</option>
</select>
Upvotes: 1
Reputation: 6160
In this solution, we're wrapping the Country text inside a span tag. Whenever we click on the dropdown, this would fill the span tag with Country property. When you're done choosing an option, this would empty the span tag.
Add this to your script;
$(document).ready(function() {
$("#countryDropdown").find("option").each(function() {
var codeIndex = $(this).text().indexOf("(");
var country = $(this).text().substring(0, codeIndex);
var code = $(this).text().substring(codeIndex);
$(this).html("<span class='select-hide' data-country='" + country + "'></span>" + code);
});
$("#countryDropdown").focus(function() {
$(this).find(".select-hide").each(function() {
$(this).text($(this).data("country"));
});
});
$("#countryDropdown").change(function() {
$(this).blur();
});
$("#countryDropdown").blur(function() {
$(this).find(".select-hide").each(function() {
$(this).text("");
});
});
});
Demo below;
$(document).ready(function() {
$("#countryDropdown").find("option").each(function() {
var codeIndex = $(this).text().indexOf("(");
var country = $(this).text().substring(0, codeIndex);
var code = $(this).text().substring(codeIndex);
$(this).html("<span class='select-hide' data-country='" + country + "'></span>" + code);
});
$("#countryDropdown").focus(function() {
$(this).find(".select-hide").each(function() {
$(this).text($(this).data("country"));
});
});
$("#countryDropdown").change(function() {
$(this).blur();
});
$("#countryDropdown").blur(function() {
$(this).find(".select-hide").each(function() {
$(this).text("");
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="countryDropdown">
<option value="3" data-code="93">
Afghanistan (93)
</option>
<option value="264" data-code="">
Åland Islands ()
</option>
<option value="4" data-code="355">
Albania (355)
</option>
<option value="5" data-code="213">
Algeria (213)
</option>
<option value="6" data-code="1">
American Samoa (1)
</option>
<option value="7" data-code="376">
Andorra (376)
</option>
<option value="8" data-code="244">
Angola (244)
</option>
<option value="9" data-code="1">
Anguilla (1)
</option>
<option value="266" data-code="">
Anonymous Proxy ()
</option>
<option value="10" data-code="672">
Antarctica (672)
</option>
<option value="11" data-code="1">
Antigua and Barbuda (1)
</option>
<option value="12" data-code="54">
Argentina (54)
</option>
<option value="13" data-code="374">
Armenia (374)
</option>
<option value="14" data-code="297">
Aruba (297)
</option>
<option value="268" data-code="">
Asia/Pacific Region ()
</option>
</select>
Upvotes: 1