<img src="" id="inputflag" class="inputflag">
<br>
<input type="text" value="1" id="inputcode" class="inputcode">
inputFlag = document.querySelectorAll('.inputflag');
inputCode = document.querySelectorAll('.inputcode');
const countries = [
{ code: '1', flag: 'https://i.ytimg.com/vi/qKYcC1nricU/maxresdefault.jpg' },
{ code: '2', flag: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Flag_of_Germany.svg/288px-Flag_of_Germany.svg.png' },
{ code: '3', flag: 'https://flagi.com.ua/wp-content/uploads/2015/10/flag_tjk.gif' }
];
inputCode.addEventListener('input', checkCountry);
window.onload = checkCountry;
document.querySelectorAll('.inputcode').onclick = checkCountry;
function checkCountry() {
const country = countries.find(n => !inputCode.value.indexOf(n.code));
inputFlag.src = country ? country.flag : '';
}
<img src="" id="inputflag" class="inputflag">
<br>
<input type="text" value="1" id="inputcode" class="inputcode">
inputFlag = $('.inputflag');
inputCode = $('.inputcode');
const countries = [
{ code: '1', flag: 'https://i.ytimg.com/vi/qKYcC1nricU/maxresdefault.jpg' },
{ code: '2', flag: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Flag_of_Germany.svg/288px-Flag_of_Germany.svg.png' },
{ code: '3', flag: 'https://flagi.com.ua/wp-content/uploads/2015/10/flag_tjk.gif' }
];
inputCode.addEventListener('input', checkCountry);
window.onload = checkCountry;
$('.inputcode').onclick = checkCountry;
function checkCountry() {
const country = countries.find(n => !inputCode.value.indexOf(n.code));
inputFlag.src = country ? country.flag : '';
}
<div class="country">
<input class="country-code">
<img class="country-flag">
</div>
function updateFlag(el) {
const country = countries.find(n => !el.value.indexOf(n.code));
el.closest('.country').querySelector('.country-flag').src = country ? country.flag : '';
}
document.addEventListener('input', function(e) {
if (e.target.classList.contains('country-code')) {
updateFlag(e.target);
}
});
document.querySelectorAll('.country-code').forEach(updateFlag);