so i am learning javascript(?), and i came across this situation i cannot really find a straight answer for.
so i have these paragraphs in an html page that i want to change the background color for, and the code I have working is this:
var colors=['red','pink','blue'];
var div2paragraphs = document.querySelectorAll('#div2 p')
for (let paragraph = 0; paragraph < div2paragraphs.length; paragraph++) {
(function(paragraph) {
div2paragraphs[paragraph].onmouseover = function(){
this.setAttribute('style', `background-color: ${colors[paragraph]}`)}
div2paragraphs[paragraph].onmouseout = function(){
this.setAttribute('style', 'background-color: white')}
})(paragraph);
}
I don't quite understand what is going on here either, I was just trying to do this with the whole "function(){}" thing (supposedly called an anonymous function?), but apparently there is something called an IIFE in javascript, so you have to WRAP the function and pass a unique variable to make it run each time within a loop, though i still don't understand why i have to end this with "(paragraph)".
I was thinking whether it is possible to do the same thing by just creating a function for each of these setAttribute actions, so i tried to write it this way:
var colors=['red','pink','blue'];
var div2paragraphs = document.querySelectorAll('#div2 p')
function changeToColor(color) {
this.setAttribute('style', 'background-color: ' + color);
}
function changeBackWhite() {
this.setAttribute('style', 'background-color: white');
}
for (let paragraph = 0; paragraph < div2paragraphs.length; paragraph++) {
div2paragraphs[paragraph].onmouseover = changeToColor('red');
div2paragraphs[paragraph].onmouseout = changeBackWhite;
}
but no matter what i do, it always pretty much comes up with the error:
Uncaught TypeError: this.setAttribute is not a function
or something along those lines. i think it has something to do with in these situations, if you add the parenthesis, it calls the function before it does anything else? so it doesn't know what "this" is? i don't quite understand, but in any case I haven't the slightest clue on how to write the same thing i did above with separate functions. would someone be able to shed some light into this...?
I am more used to Python so obviously there are fundamental concepts that are different...
PS: what are the difference between these two examples below?
for (let paragraph = 0; paragraph < div2paragraphs.length; paragraph++) {
div2paragraphs[paragraph].onmouseover = function() {
this.style['background-color'] = colors[paragraph];
}
div2paragraphs[paragraph].onmouseout = function() {
this.style['background-color'] = 'white';
}
}
and this
for (let paragraph = 0; paragraph < div2paragraphs.length; paragraph++) {
div2paragraphs[paragraph].onmouseover = function(){
this.setAttribute('style', `background-color: $(colors[paragraph]`)
}
div2paragraphs[paragraph].onmouseout = function(){
this.setAttribute('style', 'background-color: white')
}
}
and why does the first one (provided by ibrahim mahrir) work and the second one does not?