Answer the question
In order to leave comments, you need to log in
Where is the error in the counter?
I get a collection of paragraphs. I want that on click on any, the number inside increases by one.
When I prescribe the number of the paragraph manually (fragment in the comment) - everything works. When you try to write a loop so that the number increases exactly in the paragraph on which you clicked, it does not work.
Most likely a child's mistake, but my eyes are blurred and I can't see. I will be grateful for help.
var byTag = document.getElementsByTagName('p');
for (var c = 0; c<byTag.length; c++) {
byTag[c].onclick = function func () {
byTag[c].innerHTML = ++counter;
}
}
// byTag[0].onclick = function func () {
// byTag[0].innerHTML = ++counter;
// }
Answer the question
In order to leave comments, you need to log in
for (var c = 0; c<byTag.length; c++) {
byTag[c].onclick = function func () {
byTag[c].innerHTML = ++counter;
}
}
See the end of the article .
var byTag = document.getElementsByTagName('p');
var counter = 0
for (var c = 0; c < byTag.length; c++) {
byTag[c].onclick = (function (num) {
return function() {
byTag[num].innerHTML = ++counter;
}
})(c)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question