Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot set properties of null (setting 'src')

I am having a problem while making a counter webpage the code seems fine but the webpage is showing me this error in the console. Uncaught TypeError: Cannot set properties of null (setting 'src')

Pls click on this link to look at my error

here's my javascript code and Html code.

javascript:

// local reviews data

const reviews = [ { id: 1, name: "susan smith", job: "web developer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg", text: "I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry", }, { id: 2, name: "anna johnson", job: "web designer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883409/person-2_np9x5l.jpg", text: "Helvetica artisan kinfolk thundercats lumbersexual blue bottle. Disrupt glossier gastropub deep v vice franzen hell of brooklyn twee enamel pin fashion axe.photo booth jean shorts artisan narwhal.", }, { id: 3, name: "peter jones", job: "intern", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg", text: "Sriracha literally flexitarian irony, vape marfa unicorn. Glossier tattooed 8-bit, fixie waistcoat offal activated charcoal slow-carb marfa hell of pabst raclette post-ironic jianbing swag.", }, { id: 4, name: "bill anderson", job: "the boss", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883423/person-4_t9nxjt.jpg", text: "Edison bulb put a bird on it humblebrag, marfa pok pok heirloom fashion axe cray stumptown venmo actually seitan. VHS farm-to-table schlitz, edison bulb pop-up 3 wolf moon tote bag street art shabby chic. ", }, ];

const img = document.getElementById("img-container");
const author = document.getElementById("author");
const job = document.getElementById("job");
const info = document.getElementById("info");

const prevBtn = document.querySelector(".prev-btn");
const nextBtn = document.querySelector(".next-btn");
const randomBtn = document.querySelector(".random-btn");

// set current item
let currentItem = 0;

// load initial Item

window.addEventListener("DOMContentLoaded", ()=> {
const item = reviews[currentItem]
img.src = item.img;
});

HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Reviews</title>
 <link rel="stylesheet" href="./fontawesome-free-5.12.0-web/css/all.min.css">
 <link rel="stylesheet" href="styles.css">
</head>
<body>
 <main>
  <section class="container">
   <div class="title">
    <h2>our Reviews</h2>
    <div class="underline"></div>

   </div>
   <article class="review">
    <div class="img-container">
     <img src="./person-1.jpeg" id="person-img" alt="person image">
    </div>
    <h4 id="author">sara jones</h4>
    <p id="job">ux designer</p>
    <p id="info">
     Lorem, ipsum dolor sit amet consectetur adipisicing elit. Unde vitae eius facilis natus aliquid accusantium cum distinctio cupiditate animi numquam?
    </p>
    <div class="button-container">
     <button class="prev-btn">
      <i class="fas fa-chevron-left"></i>
     </button>
      <button class="next-btn">
      <i class="fas fa-chevron-right"></i>
     </button>
    </div>
     <button class="random-btn">suprise me</button>
    

   </article>
  </section>
 </main>
 <script src="app.js"></script>
</body>
</html>
like image 205
Rajat Sinha Avatar asked Oct 23 '25 03:10

Rajat Sinha


1 Answers

first of all as aerial301 said you are not targeting the img tag but the div tag.

secondly you should change the html from <div class="img-container"> to <div id="img-container"> you were targeting the div with class img-container no an id

const reviews = [ { id: 1, name: "susan smith", job: "web developer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg", text: "I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry", }, { id: 2, name: "anna johnson", job: "web designer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883409/person-2_np9x5l.jpg", text: "Helvetica artisan kinfolk thundercats lumbersexual blue bottle. Disrupt glossier gastropub deep v vice franzen hell of brooklyn twee enamel pin fashion axe.photo booth jean shorts artisan narwhal.", }, { id: 3, name: "peter jones", job: "intern", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg", text: "Sriracha literally flexitarian irony, vape marfa unicorn. Glossier tattooed 8-bit, fixie waistcoat offal activated charcoal slow-carb marfa hell of pabst raclette post-ironic jianbing swag.", }, { id: 4, name: "bill anderson", job: "the boss", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883423/person-4_t9nxjt.jpg", text: "Edison bulb put a bird on it humblebrag, marfa pok pok heirloom fashion axe cray stumptown venmo actually seitan. VHS farm-to-table schlitz, edison bulb pop-up 3 wolf moon tote bag street art shabby chic. ", }, ];

const img = document.getElementById("img-container");
const author = document.getElementById("author");
const job = document.getElementById("job");
const info = document.getElementById("info");

const prevBtn = document.querySelector(".prev-btn");
const nextBtn = document.querySelector(".next-btn");
const randomBtn = document.querySelector(".random-btn");

// set current item
let currentItem = 0;

// load initial Item

window.addEventListener("DOMContentLoaded", ()=> {
const item = reviews[currentItem]
img.src = item.img;
});
<main>
  <section class="container">
   <div class="title">
    <h2>our Reviews</h2>
    <div class="underline"></div>

   </div>
   <article class="review">
    <div id="img-container">
     <img src="./person-1.jpeg" id="person-img" alt="person image">
    </div>
    <h4 id="author">sara jones</h4>
    <p id="job">ux designer</p>
    <p id="info">
     Lorem, ipsum dolor sit amet consectetur adipisicing elit. Unde vitae eius facilis natus aliquid accusantium cum distinctio cupiditate animi numquam?
    </p>
    <div class="button-container">
     <button class="prev-btn">
      <i class="fas fa-chevron-left"></i>
     </button>
      <button class="next-btn">
      <i class="fas fa-chevron-right"></i>
     </button>
    </div>
     <button class="random-btn">suprise me</button>
    

   </article>
  </section>
 </main>
like image 86
Alan Omar Avatar answered Oct 25 '25 17:10

Alan Omar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!