Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was making a simple project of Counter.
In this I,am unable to use nextElementSibling method to access the next sibling element.

What I have tried:

ON WEB

Counter



counter
0

decrease
reset
increase





HTML DOCUMENT
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Counter</title>

    <!-- styles -->
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <main>
      <div class="container">
        <h1>counter</h1>
        <span id="value">0</span>
        <div class="button-container">
          <button class="btn decrease">decrease</button>
          <button class="btn reset">reset</button>
          <button class="btn increase">increase</button>
        </div>
      </div>
    </main>
    <!-- javascript -->
    <script src="app.js"></script>
  </body>
</html>



JS DOCUMENT
let value = document.getElementById("value");
//console.log(value);
let count = 0;
const parent = document.getElementsByClassName("button-container");
const button_dec = parent.firstChildElement;
const button_reset = button_dec.nextSibling;
const button_inc = button_reset.lastChildElement;
//console.log(button_dec);
//console.log(button_inc);
//console.log(button_reset);
button_dec.addEventListener("click",function () {
    count--;
    let value_dec = value.innerHTML;
    value_dec = count;
})

button_reset.addEventListener("click",function () {
    count = 0;
    let value_dec = value.innerHTML;
    value_dec = count;
})

button_inc.addEventListener("click",function () {
    count++;
    let value_dec = value.innerHTML;
    value_dec = count;
})



ERROR
app.js:35 Uncaught TypeError: Cannot read properties of undefined (reading 'nextSibling')
Posted
Updated 29-Jan-23 16:43pm

1 solution

Well, it's telling you that the object you're calling a property getter or method on doesn't exist, or in javascript terms, undefined.

So, work backwards from the point of the error. Why does button_dec not refer to an object? Probably because the call to parent.firstChildElement didn't return an element, but instead couldn't find a child element. Why did that happen? Probably because your call to getElementsByClassName didn't return anything, or didn't return the element you think it did.

You'll have to insert console.log statements after each step in the code to see what it returned, like this:
JavaScript
const parent = document.getElementsByClassName("button-container");
console.log(parent);
const button_dec = parent.firstChildElement;
console.log(button_dec);
const button_reset = button_dec.nextSibling;
console.log(button_reset);
const button_inc = button_reset.lastChildElement;

So why did firstChildElement return undefined? Google for "javascript firstChildElement" and you'll find something interesting. Look very closely at the name of the function you're calling.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900