I have 3 selectboxes, all of them have the same class. In the selectbox I am trying to do, if the first item is selected, the text color should be gray, if the others are selected, the text color should be white.
I tried this with jquery and tailwindcss.
MY HTML CODE :
<div class="text-white grid grid-cols-2 px-4 gap-4">
<select class="focus:outline-none focus:ring-0 border-gray-500 rounded-md bg-transparent first-child-gray-select text-gray-500" required>
<option value="" class="bg-primary text-gray-500" disabled hidden selected>Ülke...</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
<option value="tr" class="bg-primary text-white">Türkiye</option>
</select>
<select class="focus:outline-none focus:ring-0 border-gray-500 rounded-md bg-transparent first-child-gray-select text-gray-500" required>
<option value="" class="bg-primary text-gray-500" disabled hidden selected>Şehir...</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
<option value="tr" class="bg-primary text-white">Bursa</option>
</select>
<select class="focus:outline-none focus:ring-0 border-gray-500 rounded-md bg-transparent first-child-gray-select text-gray-500" required>
<option value="" class="bg-primary text-gray-500" disabled hidden selected>İlçe...</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
<option value="tr" class="bg-primary text-white">İznik</option>
</select>
</div>
MY CSS CODE :
[hidden] {
display: none;
}
MY JS CODE : (JQUERY)
$(".first-child-gray-select").each((select) => {
$(select).on("change", function () {
console.log("sea");
if ($(select + "option:selected").hasClass("text-white")) {
$(select).removeClass("text-gray-500");
$(select).addClass("text-white");
}
});
});
When I edit the code in this way, when I select the option, it changes the color of all the select boxes.
$(".first-child-gray-select").change(function () {
if ($(".first-child-gray-select option:selected").hasClass("text-white")) {
$(".first-child-gray-select").removeClass("text-gray-500");
$(".first-child-gray-select").addClass("text-white");
}
});