I'm a beginner in JavaScript, I've read every answer in similar topics, I still can't understand what's really going on because no one explained the part that I'm confused about.
I have a HTML document with two paragraphs, and this is what I'm using to change the color of a paragraph to red
when I click on it :
var func = function() {
/*Line 1*/ var paragraphs = document.querySelectorAll('p')
/*Line 2*/
/*Line 3*/ for (var i = 0; i < paragraphs.length; i++) {
/*Line 4*/ p = paragraphs[i]
/*Line 5*/ p.addEventListener('click', function() {
/*Line 6*/ p.classList.toggle('red')
/*Line 7*/ })
/*Line 8*/ }
}
func();
The result is, where ever I click, only the last paragraph changes color to red. All the answers to questions similar to mine said that after the For
loop is finished, the value of i
will be 1
, so that's what the closure will use, and then the eventListener
will be added to same second paragraph ? And that I can fix this using a Immediately-Invoked Function
or let
to make i
private to the closure, I don't know why should I make ì
private if the closure has access to it at each iteration of the loop..
I just can't understand what's really going on here, isn't the loop executed line after another? At the start i
will have a value of 0
, so at Line 4
the variable p
will have the first paragraph, then at Line 5-6
the function will use that p
and attach the listener to it, then the loop gets executed a second time and i
will have a value of 1
, then at Line 5-6
again the closure gets the new value of p
?
I know the closure have access to the global variables here like i
, so it have access to the p
at Line 4
when its value changes.
What am I missing here please? Thank you very much in advance!