I recently started using online jQuery to manage images within the "select" element of my HTML code. However, I've encountered a problem where events on each individual "select" element are not being separated correctly.
var selectedId = "";
var langArray = [];
$(".box").each(function () {
selectedId = $(this).attr("id");
$(this)
.find(".vodiapicker option")
.each(function () {
var img = $(this).attr("data-thumbnail");
var text = this.innerText;
var value = $(this).val();
var item =
'<li><img src="' +
img +
'" alt="" value="' +
value +
'"/><span>' +
text +
"</span></li>";
langArray.push(item);
});
$(this).find("#a").html(langArray);
//Set the button value to the first el of the array
$(this).find(".btn-select").html(langArray[0]);
$(this).find(".btn-select").attr("value", "en");
});
//change button stuff on click
$("li").click(function () {
var img = $(this).find("img").attr("src");
var value = $(this).find("img").attr("value");
var text = this.innerText;
var item =
'<li><img src="' + img + '" alt="" /><span>' + text + "</span></li>";
$(".btn-select").html(item);
$(".btn-select").attr("value", value);
$(".b").toggle();
//console.log(value);
});
$(".btn-select").click(function () {
$(".b").toggle();
});
//check local storage for the lang
var sessionLang = localStorage.getItem("lang");
if (sessionLang) {
//find an item with value of sessionLang
var langIndex = langArray.indexOf(sessionLang);
$(".btn-select").html(langArray[langIndex]);
$(".btn-select").attr("value", sessionLang);
} else {
var langIndex = langArray.indexOf("ch");
$(".btn-select").html(langArray[langIndex]);
//$('.btn-select').attr('value', 'en');
}
.vodiapicker {
display: none;
}
#a {
padding-left: 0px;
}
#a img,
.btn-select img {
width: 18px;
}
#a li {
list-style: none;
padding-top: 5px;
padding-bottom: 5px;
}
#a li:hover {
background-color: #f4f3f3;
}
#a li img {
margin: 5px;
}
#a li span,
.btn-select li span {
margin-left: 30px;
}
/* item list */
.b {
display: none;
width: 100%;
max-width: 350px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 5px;
}
.open {
display: show !important;
}
.btn-select {
margin-top: 10px;
width: 100%;
max-width: 350px;
height: 34px;
border-radius: 5px;
background-color: #fff;
border: 1px solid #ccc;
}
.btn-select li {
list-style: none;
float: left;
padding-bottom: 0px;
}
.btn-select:hover li {
margin-left: 0px;
}
.btn-select:hover {
background-color: #f4f3f3;
border: 1px solid transparent;
box-shadow: inset 0 0px 0px 1px #ccc;
}
.btn-select:focus {
outline: none;
}
.lang-select {
margin-left: 50px;
}
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"
></script>
<div class="box" id="one">
<select class="vodiapicker">
<option>Select one</option>
<option value="en" class="test" data-thumbnail="images/3.png">
English
</option>
<option value="au" data-thumbnail="images/3.png">Engllish (AU)</option>
</select>
<div class="lang-select">
<button class="btn-select" value=""></button>
<div class="b">
<ul id="a"></ul>
</div>
</div>
</div>
<div class="box" id="two">
<select class="vodiapicker">
<option>Select one</option>
<option value="en" class="test" data-thumbnail="images/3.png">
french
</option>
<option value="au" data-thumbnail="images/3.png">french F</option>
</select>
<div class="lang-select">
<button class="btn-select" value=""></button>
<div class="b">
<ul id="a"></ul>
</div>
</div>
</div>
Upon clicking on a select element, both elements open simultaneously and additionally, the HTML content is displayed separately for each div. However, when selecting dropdown two, it also displays the content from dropdown one.