I don't understand how to correct this

153 views Asked by At

Hey I'm a new coder and I'm using freecode camp to learn js. I'm currently on the calorie counter exercise and I'm duplicating the code in VsCode. I just added an addEntry button to the js using a query selector and I'm getting this error:

Uncaught TypeError TypeError: Cannot read properties of null (reading 'querySelectorAll') at addEntry - filepath calorie counter.js 21

I'm a complete noob and I don't know what to search for.

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="device=device-width, initial-scale= 1.0" />\
        <link rel="stylesheet" href="C:\Users\babat\OneDrive\Desktop\Vs Code Projects\JsApps\Calorie Counter.css" />
        <title>Calorie Counter</title>
    </head>
    <body>
        <main>
            <h1>Calorie Counter</h1>
            <div class="container">
                <form id="calorie-counter">
                    <label for="budget">Budget</label>
                    <input id="budget" type="number" min="0" placeholder="Daily Calorie Counter" required />
                    <fieldset class="breakfast">
                        <legend>Breakfast</legend>
                        <div class="input-container"></div>
                    </fieldset>
                    <fieldset id="lunch">
                        <legend>Lunch</legend>
                        <div class="input-container"></div>
                    </fieldset>
                    <fieldset id="dinner">
                        <legend>Dinner</legend>
                        <div class="input-container"></div>
                    </fieldset>
                    <fieldset id="snacks">
                        <legend>Snacks</legend>
                        <div class="input-container"></div>
                    </fieldset>
                    <fieldset id="exercise">
                        <legend>Exercise</legend>
                        <div class="input-container"></div>
                    </fieldset>
                    <div class="controls">
                        <span>
                            <label for="entry-dropdown">Add food or exercise</label>
                            <select id="entry-dropdown" name="options">
                                <option value="breakfast" selected>Breakfast</option>
                                <option value="lunch">Lunch</option>
                                <option value="dinner">Dinner</option>
                                <option value="snacks">Snacks</option>
                                <option value="exercise">Exercise</option>
                            </select>
                            <button id="add-entry" type="button">Add Entry</button>
                        </span>
                    </div>
                    <div>
                        <button type="submit">Calculate Remaining Calories</button>
                        <button id="clear" type="button">Clear</button>
                    </div>
                </form>
                <div id="output" class="output hide"></div>
            </div>
        </main>
        <script src="C:\Users\babat\OneDrive\Desktop\Vs Code Projects\JsApps\Calorie Counter.js"></script>
    </body>
</html>

css:

:root {
    --light-grey: #f5f6f7;
    --dark-blue: #0a0a23;
    --fcc-blue: #1b1b32;
    --light-yellow: #fecc4c;
    --dark-yellow: #feac32;
    --light-pink: #ffadad;
    --dark-red: #850000;
    --light-green: #acd157;
  }
  
  body {
    font-family: "Lato", Helvetica, Arial, sans-serif;
    font-size: 18px;
    background-color: var(--fcc-blue);
    color: var(--light-grey);
  }
  
  h1 {
    text-align: center;
  }
  
  .container {
    width: 90%;
    max-width: 680px;
  }
  
  h1,
  .container,
  .output {
    margin: 20px auto;
  }
  
  label,
  legend {
    font-weight: bold;
  }
  
  .input-container {
    display: flex;
    flex-direction: column;
  }
  
  button {
    outline: none;
    cursor: pointer;
    text-decoration: none;
    background-color: var(--light-yellow);
    border: 2px solid var(--dark-yellow);
  }
  
  .clear {
    background-color: var(--light-pink);
    color: var(--dark-red);
    border-color: var(--dark-red);
  }
  
  button,
  input,
  select {
    min-height: 24px;
    color: var(--dark-blue);
  }
  
  fieldset,
  label,
  button,
  input,
  select {
    margin-bottom: 10px;
  }
  
  .output {
    border: 2px solid var(--light-grey);
    padding: 10px;
    text-align: center;
  }
  
  .hide {
    display: none;
  }
  
  .output span {
    font-weight: bold;
    font-size: 1.2em;
  }
  
  .surplus {
    color: var(--light-green);
  }
  
  .deficit {
    color: var(--light-pink);
  }

java:

const calorieCounter = document.getElementById('calorie-counter');
const budgetNumberInput = document.getElementById('budget');
const entryDropdown = document.getElementById('entry-dropdown');
const addEntryButton = document.getElementById('add-entry');
const clearButton = document.getElementById('clear');
const output = document.getElementById('output');
let isError = false;

function cleanInputString(str) {
  const regex = /[+-\s]/g;
  return str.replace(regex, '');
}

function isInvalidInput(str) {
  const regex = /\d+e\d+/i;
  return str.match(regex);
}

function addEntry() {
  const targetInputContainer = document.querySelector(`#${entryDropdown.value} .input-container`);
  const entryNumber = targetInputContainer.querySelectorAll('input[type="text"]').length + 1;
  const HTMLString = `
  <label for="${entryDropdown.value}-${entryNumber}-name">Entry ${entryNumber} Name</label>
  <input type="text" id="${entryDropdown.value}-${entryNumber}-name" placeholder="Name" />
  <label for="${entryDropdown.value}-${entryNumber}-calories">Entry ${entryNumber} Calories</label>
  <input
    type="number"
    min="0"
    id="${entryDropdown.value}-${entryNumber}-calories"
    placeholder="Calories"
  />`;
  targetInputContainer.insertAdjacentHTML('beforeend', HTMLString);
}



addEntryButton.addEventListener("click", addEntry);

The code works in the preview window of the freecode camp app but when I run it in visual studio code, it doesn't work and throws the error. I tried copying and pasting the code straight from the app into vs code but I still get the error.

1

There are 1 answers

1
Victoria Udechukwu On BEST ANSWER

In your HTMl you added a class="breakfast" on the first fieldset element

 <fieldset class="breakfast">
        <legend>Breakfast</legend>
        <div class="input-container"></div>
  </fieldset>

where as in your javascript code you're trying to retrieve it by the id, targetInputContainer is null as a result and this is the cause of the error Cannot read properties of null

  const targetInputContainer = document.querySelector(`#${entryDropdown.value} .input-container`);

The solution is to change your the class="breakfast" to id="breakfast" in your html

<fieldset id="breakfast">
        <legend>Breakfast</legend>
        <div class="input-container"></div>
</fieldset>