Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  17] [ 3]  / answers: 1 / hits: 40525  / 10 Years ago, fri, january 16, 2015, 12:00:00

I want to use something similar to the following to clear errors in a form upon a resubmission attempt:



document.querySelectorAll(#form-error-name, #form-error-email, #form-error-tel, #form-error-dob, #form-error-password, #form-error-goal).innerHTML= ;


...But the contents of the divs isn't cleared. What am I doing wrong?


More From » javascript

 Answers
7

You'll need to loop through the results



    var errors = document.querySelectorAll(
#form-error-name,
#form-error-email,
#form-error-tel,
#form-error-dob,
#form-error-password,
#form-error-goal);

[].forEach.call(errors, function(error) {
error.innerHTML = '';
});


querySelectorAll doesn't return an array, but a node list, which doesn't have a forEach method on its prototype.



The loop above is using the forEach method on the array object's prototype on the nodeList object.


[#68188] Wednesday, January 14, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristiano

Total Points: 652
Total Questions: 94
Total Answers: 108

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;