Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  196] [ 3]  / answers: 1 / hits: 26595  / 9 Years ago, sat, october 24, 2015, 12:00:00

Why does this code throw an error in the console reading TypeError: pizzaBox.querySelector is not a function. (In 'pizzaBox.querySelector('h6')', 'pizzaBox.querySelector' is undefined)?



function addToppingsToAll (toppings)
{
var pizzaBoxHolder = document.getElementById(PizzaBoxHolder);
var PizzaBoxList = pizzaBoxHolder.childNodes;
for ( var i = 0 ; i < pizzaBoxList.length ; i++ )
{
var pizzaBox = pizzaBoxList[i];
toppingList = pizzaBox.querySelector('h6');
toppingList.textContent = You have + toppings on your pizza;
}
}

More From » dom

 Answers
180

There are at least three isssues in your code:




  1. You are probably iterating through some text nodes which don't have a .querySelector() method.

  2. You are not initializing your for loop iteration variable i

  3. You have an undeclared variable lineBoxList you are attempting to use.



You can simplify things by just using .querySelectorAll() and letting the selector do more of the work for you.



function addToppingsToAll (toppings) {
var toppingItems = document.querySelectorAll(#PizzaBoxHolder h6);
for (var i = 0; i < toppingItems.length; i++) {
toppingItems[i].textContent = You have + toppings on your pizza;
}
}

[#64611] Thursday, October 22, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
magaly

Total Points: 524
Total Questions: 96
Total Answers: 89

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;