Darkening an image when clicked is requiring an initial click on that image, and I don’t know why

I’m trying to make an item tracker for a game as a webapp. I wanted to have the icon of each item displayed in a grid, and when you click on the image it darkens to indicate that you’ve gotten it. So far I’ve only figured out how to affect the opacity of the images, but works well enough for now. Though when the opacity starts at 1.0, it requires a second click to activate, but only for the first time. After that it works as intended, toggling on click. If I set the initial opacity to 0.5, it works on the first click.

I made a table to place the images in a grid (added 8 for the time being) and gave each one a unique id, and the class “Loot” like so:

function lootDarken(x) {
  image = document.getElementById(x);
  if (image.style.opacity === "1") {
    image.style.opacity = "0.5";
  } else {
    image.style.opacity = "1";
  }
}
<table>
  <tr>
    <td><img onClick="lootDarken('Loot1')" id="Loot1" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot2')" id="Loot2" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot3')" id="Loot3" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot4')" id="Loot4" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot5')" id="Loot5" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot6')" id="Loot6" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot7')" id="Loot7" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot8')" id="Loot8" class="Loot" src="https://picsum.photos/200/300"></td>
  </tr>
</table>

Also, if there’s a cleaner/easier way to place these images in a grid, please share. I have to go through hundreds of these.

I thought maybe it was because the opacity wasn’t officially set, and that clicking it the first only set the opacity to 1 since technically, the opacity wasn’t 1. So I added the class “Loot” in order to set the opacity on startup. That didn’t work, so I tried setting the initial opacity to 0.5 just to see what would happen and that worked wonderfully.

I also tried reversing the function, making it check if the opacity is not 1:

function lootDarken(x) {
    
    image = document.getElementById(x);
    if (image.style.opacity != "1") {
        image.style.opacity = "1";
    } else {
        image.style.opacity = "0.5";
    }
    
}

But that led to the same result. I don’t even know what is happening that’s causing this weird interaction, so I came here hoping for an explanation… and maybe tips on how to make it less repetitive.

New contributor

Sintoxin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

Because element.style refers to the inline style of the element, not computed style.

To get around this, either assign all images with an initial inline opacity, or check their live style instead.

// --- Option 1: Check for inline style ---
function lootDarken(x) {
  image = document.getElementById(x);
  if (image.style.opacity === "1") {
    image.style.opacity = "0.5";
  } else {
    image.style.opacity = "1";
  }
}
// --- Option 2: Check for computed style ---
function lootDarken2(x) {
  const image = document.getElementById(x);
  const style = window.getComputedStyle(image);
  if (style.opacity === "1") {
    image.style.opacity = "0.5";
  } else {
    image.style.opacity = "1";
  }
}
// --- Option 1.2: You can also set inline style programmatically ---
document.addEventListener("DOMContentLoaded", () => {
  document.querySelectorAll('img[id^="Loot"]').forEach((img) => {
    img.style.opacity = "1";
  });
});
<table>
  <tr>
    <!-- Option 1.1: You can add inline style by hand -->
    <td><img onClick="lootDarken('Loot1')" id="Loot1" class="Loot" src="https://picsum.photos/200/300" style="opacity:1"></td>
    <!-- Option 2: Or use computed style instead -->
    <td><img onClick="lootDarken2('Loot2')" id="Loot2" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot3')" id="Loot3" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot4')" id="Loot4" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot5')" id="Loot5" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot6')" id="Loot6" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot7')" id="Loot7" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot8')" id="Loot8" class="Loot" src="https://picsum.photos/200/300"></td>
  </tr>
</table>

The problem with the original snippet is that image.style.opacity is not set initially:

const img = document.querySelector('img');
img.onclick = function() {
  console.log('Opacity:'+this.style.opacity);
  console.log(this.style.opacity == '' ? 1 : 0);
}
<img src="https://picsum.photos/200/300">

You can solve your problem by adjusting the if-statement:

function lootDarken(x) {
  image = document.getElementById(x);
  if (image.style.opacity === "1" || image.style.opacity === "") {
    image.style.opacity = "0.5";
  } else {
    image.style.opacity = "1";
  }
}

function lootDarken(x) {
  image = document.getElementById(x);
  if (image.style.opacity === "1" || image.style.opacity === "") {
    image.style.opacity = "0.5";
  } else {
    image.style.opacity = "1";
  }
}
<table>
  <tr>
    <td><img onClick="lootDarken('Loot1')" id="Loot1" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot2')" id="Loot2" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot3')" id="Loot3" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot4')" id="Loot4" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot5')" id="Loot5" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot6')" id="Loot6" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot7')" id="Loot7" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img onClick="lootDarken('Loot8')" id="Loot8" class="Loot" src="https://picsum.photos/200/300"></td>
  </tr>
</table>

Instead of trying to modify the opacity with the style attribute, you could also toggle a class which lowers the opacity. By doing so you can also add a CSS transition to the effect

const loots = document.querySelectorAll('img.Loot');

loots.forEach(function(loot, index) {
  loot.onclick = function() {
    this.classList.toggle('dark');
  };
});
img.Loot {
  opacity: 1;
  transition: opacity 0.5s ease-in-out;
}

img.Loot.dark {
  opacity: 0.5;
}
<table>
  <tr>
    <td><img id="Loot1" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot2" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot3" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot4" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot5" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot6" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot7" class="Loot" src="https://picsum.photos/200/300"></td>
    <td><img id="Loot8" class="Loot" src="https://picsum.photos/200/300"></td>
  </tr>
</table>

2

Please use the code below:

    function lootDarken(id) {
      const image = document.getElementById(id);
      image.classList.toggle('clk-collect');
    }
    .img-sec {
      opacity: 1;
      transition: opacity 0.3s ease;
    }
    .clk-collect {
      opacity: 0.5;
    }
    .grid-wrapper {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 10px;
    }
    img {
      width: 100%;
      height: auto;
    }
<div class="grid-wrapper">
    <img onClick="lootDarken('Loot1')" id="Loot1" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot2')" id="Loot2" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot3')" id="Loot3" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot4')" id="Loot4" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot5')" id="Loot5" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot6')" id="Loot6" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot7')" id="Loot7" class="img-sec" src="https://picsum.photos/200/300">
    <img onClick="lootDarken('Loot8')" id="Loot8" class="img-sec" src="https://picsum.photos/200/300">
  </div>
New contributor

Nidhit34 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật