Как при выборе страны сделать чтобы менялся флаг?
подскажите пожалуйста как сделать чтобы при выборе страны менялся флаг ?
Ответы (3 шт):
Автор решения: De.Minov
→ Ссылка
Вариант в лоб, которое заменяет атрубут class у блока с флагом на number-select__wraps number-select__ + ID выбранного "option".
var numberSelect = $('.number-select__list');
var numberSelectFlag = $('.number-select__flag');
var citiesDropdown = $('.cities-dropdown__menu');
numberSelectFlag.click(function(list) {
numberSelect.slideToggle('fast');
list.stopPropagation();
});
if (window.matchMedia('(max-width: 767px)').matches) {
numberSelectFlag.click(function() {
citiesDropdown.slideUp('fast');
});
}
$(document).on('click', '.number-select__item', function() {
let country = $(this).find('.number-select__country-name').text();
let code = $(this).find('.number-select__dial-code').text();
let flag = $('.number-select__output .number-select__wraps');
flag.attr('class', 'number-select__wraps number-select__'+$(this).attr('id').toLowerCase());
$('.number-select__selected-dial-code').text(`${code} (${country})`);
numberSelect.slideToggle('fast');
});
$(document).click(function() {
numberSelect.slideUp('fast');
});
.number-select__flag {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: relative;
display: flex;
align-items: center;
width: 274px;
height: 48px;
border-radius: 5px;
/*margin-left: 12px;*/
outline: 0;
border: 0;
box-shadow: none;
flex: 1;
padding: 0 1em;
color: #383839;
background-image: none;
cursor: pointer;
background: #FFFFFF url(../svg/phone-search-arrow.svg) no-repeat 95.4% 51%;
border: 1px solid #DADADA;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.number-select__list {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: absolute;
z-index: 2;
display: none;
width: 302px;
max-height: 250px;
white-space: nowrap;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
margin-right: 25px;
/*margin-left: 25px;*/
cursor: pointer;
border: 1px solid #DADADA;
box-sizing: border-box;
border-radius: 5px;
background: #FFFFFF;
box-shadow: 0px 3px 40px rgba(0, 0, 0, 0.08);
margin-top: 5px;
}
.cities-dropdown__menu {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: absolute;
top: 100%;
left: 0;
z-index: 2;
display: none;
width: 302px;
max-height: 250px;
white-space: nowrap;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
margin-right: 25px;
/*margin-left: 25px;*/
cursor: pointer;
border: 1px solid #DADADA;
box-sizing: border-box;
border-radius: 5px;
background: #FFFFFF;
box-shadow: 0px 3px 40px rgba(0, 0, 0, 0.08);
margin-top: 5px;
}
.cities-dropdown__menu li {
display: flex;
align-items: center;
padding: 17px;
}
.cities-dropdown__menu li:hover {
background: #EEEEEE;
border-radius: 0px;
}
.number-select__item {
display: flex;
align-items: center;
padding: 15px;
}
.number-select__dial-code {
color: #A0A0A0;
padding-left: 5px;
padding-top: 1px;
}
.number-select__country-name {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
line-height: 17px;
color: #383839;
padding-top: 1px;
}
.number-select__selected-dial-code {
padding-top: 1px;
line-height: 17px;
}
.number-select__output {
display: flex;
}
.number-select__us {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-united-states-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__uk {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-united-kingdom-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__ar {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-argentina-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__au {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-australia-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="number-area">
<div class="col-lg-12 col-md-12 col-12 number number-box">
<div class="number-search">
<div class="number-dropdown">
<div class="number-select">
<div class="number-select__flag">
<div class="number-select__output">
<div class="number-select__wraps number-select__us"></div>
<div class="number-select__selected-dial-code">+1 (United States)</div>
</div>
</div>
<ul class="number-select__list">
<li class="number-select__item" id="UK">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__uk">
</div>
</div>
<span class="number-select__country-name">United Kingdom</span>
<span class="number-select__dial-code">+44</span>
</li>
<li class="number-select__item" id="US">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__us">
</div>
</div>
<span class="number-select__country-name">United States</span>
<span class="number-select__dial-code">+1</span>
</li>
<li class="number-select__item" id="AR">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__ar">
</div>
</div>
<span class="number-select__country-name">Argentina</span>
<span class="number-select__dial-code">+54</span>
</li>
<li class="number-select__item" id="AU">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__au">
</div>
</div>
<span class="number-select__country-name">Australia</span>
<span class="number-select__dial-code">+61</span>
</li>
</ul>
Автор решения: EzioMercer
→ Ссылка
Как я понял у вас одинаковые классы для отображения флагов, если они всегда будут иметь одинаковый набор классов, то пу сути вам надо добавить всего 1 строчку numberSelectFlagIco.attr('class', target.find('.number-select__wraps').attr('class'));. Просто забрать все классы флага у нажатого и поставить вместо классов у уже выбранного:
var numberSelect = $('.number-select__list');
var numberSelectFlag = $('.number-select__flag');
var numberSelectFlagIco = $('.number-select__flag .number-select__wraps');
var citiesDropdown = $('.cities-dropdown__menu');
numberSelectFlag.click(function(list) {
numberSelect.slideToggle('fast');
list.stopPropagation();
});
if (window.matchMedia('(max-width: 767px)').matches) {
numberSelectFlag.click(function() {
citiesDropdown.slideUp('fast');
});
}
$(document).on('click', '.number-select__item', function() {
const target = $(this);
let country = target.find('.number-select__country-name').text();
let code = target.find('.number-select__dial-code').text();
numberSelectFlagIco.attr('class', target.find('.number-select__wraps').attr('class'));
$('.number-select__selected-dial-code').text(`${code} (${country})`);
numberSelect.slideToggle('fast');
});
$(document).click(function() {
numberSelect.slideUp('fast');
});
.number-select__flag {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: relative;
display: flex;
align-items: center;
width: 274px;
height: 48px;
border-radius: 5px;
/*margin-left: 12px;*/
outline: 0;
border: 0;
box-shadow: none;
flex: 1;
padding: 0 1em;
color: #383839;
background-image: none;
cursor: pointer;
background: #FFFFFF url(../svg/phone-search-arrow.svg) no-repeat 95.4% 51%;
border: 1px solid #DADADA;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.number-select__list {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: absolute;
z-index: 2;
display: none;
width: 302px;
max-height: 250px;
white-space: nowrap;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
margin-right: 25px;
/*margin-left: 25px;*/
cursor: pointer;
border: 1px solid #DADADA;
box-sizing: border-box;
border-radius: 5px;
background: #FFFFFF;
box-shadow: 0px 3px 40px rgba(0, 0, 0, 0.08);
margin-top: 5px;
}
.cities-dropdown__menu {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
position: absolute;
top: 100%;
left: 0;
z-index: 2;
display: none;
width: 302px;
max-height: 250px;
white-space: nowrap;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
margin-right: 25px;
/*margin-left: 25px;*/
cursor: pointer;
border: 1px solid #DADADA;
box-sizing: border-box;
border-radius: 5px;
background: #FFFFFF;
box-shadow: 0px 3px 40px rgba(0, 0, 0, 0.08);
margin-top: 5px;
}
.cities-dropdown__menu li {
display: flex;
align-items: center;
padding: 17px;
}
.cities-dropdown__menu li:hover {
background: #EEEEEE;
border-radius: 0px;
}
.number-select__item {
display: flex;
align-items: center;
padding: 15px;
}
.number-select__dial-code {
color: #A0A0A0;
padding-left: 5px;
padding-top: 1px;
}
.number-select__country-name {
font-family: "Fira Sans";
font-style: normal;
font-weight: 500;
font-size: 14px;
letter-spacing: 0.008em;
line-height: 17px;
color: #383839;
padding-top: 1px;
}
.number-select__selected-dial-code {
padding-top: 1px;
line-height: 17px;
}
.number-select__output {
display: flex;
}
.number-select__us {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-united-states-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__uk {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-united-kingdom-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__ar {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-argentina-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
.number-select__au {
background-image: url('https://dialics-site.intertech.dev/wp-content/themes/dialics/assets/svg/flag/flag-australia-min.svg');
background-repeat: no-repeat;
background-size: cover;
width: 20px;
height: 20px;
margin-right: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="number-area">
<div class="col-lg-12 col-md-12 col-12 number number-box">
<div class="number-search">
<div class="number-dropdown">
<div class="number-select">
<div class="number-select__flag">
<div class="number-select__output">
<div class="number-select__wraps number-select__us"></div>
<div class="number-select__selected-dial-code">+1 (United States)</div>
</div>
</div>
<ul class="number-select__list">
<li class="number-select__item" id="GB">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__uk">
</div>
</div>
<span class="number-select__country-name">United Kingdom</span>
<span class="number-select__dial-code">+44</span>
</li>
<li class="number-select__item" id="US">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__us">
</div>
</div>
<span class="number-select__country-name">United States</span>
<span class="number-select__dial-code">+1</span>
</li>
<li class="number-select__item" id="AR">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__ar">
</div>
</div>
<span class="number-select__country-name">Argentina</span>
<span class="number-select__dial-code">+54</span>
</li>
<li class="number-select__item" id="AU">
<div class="number-select__flag-box">
<div class="number-select__wraps number-select__au">
</div>
</div>
<span class="number-select__country-name">Australia</span>
<span class="number-select__dial-code">+61</span>
</li>
</ul>
Автор решения: soledar10
→ Ссылка
Как вариант - https://github.com/jackocnr/intl-tel-input
Пример
<form action="#" class="form">
<div class="form__control">
<input type="tel" id="phone">
</div>
</form>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/build/css/intlTelInput.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/js/intlTelInput.min.js"></script>
<script>
const input = document.querySelector("#phone");
window.intlTelInput(input, {
preferredCountries: ['us', 'gb', 'ar', 'au'],
//onlyCountries: ['us', 'gb', 'ar', 'au'],
utilsScript: "https://cdn.jsdelivr.net/npm/[email protected]/build/js/utils.js",
});
</script>