So the problem I'm having is that the console is saying that display
is undefined when editStyle(list, display, "list-item");
should return:
function(){
if(list[0]===undefined) {
list.style.display="list-item"
}
else {
for(var i=0; i<list.length; i++) {
list[i].style.display="list-item"
}
}
}
The code:
var title=document.getElementsByClassName('title');
var list=document.getElementsByClassName('list');
var test=document.getElementById("testText");
function editStyle(element, prop, newVal){
//returns a function which changes the elements prop to newVal
return function(){if(element[0]===undefined){element.style.prop=newVal;}
//should check if prop is a list of elements or a single element
else {for(var i=0; i<element.length; i++){
element[i].style.prop=newVal;}
}};
};
function onEvent(element, event, action){
if(element[0]==undefined){ element.event=action;}
else{for(var i=0; i<element.length; i++){
element[i].event=action;
}}
}
onEvent(title, onmouseover, editStyle(list, display,"list-item"));
//should set the title.onmouseover event to equal the function out put of editStyle
// which should change the list.style.display value to "list-item"
Here is the working code with the help of the brilliant commentators:
function editStyle(element, prop, newVal){
//returns a function which changes the elements prop to newVal
return function(){if(element[0]===undefined){element.style[prop]=newVal;}
//should check if prop is a list of elements or a single element
else {for(var i=0; i<element.length; i++){
element[i].style[prop]=newVal;}
}};
};
function onEvent(element, event, action){
if(element[0]==undefined){ element.event=action;}
else{for(var i=0; i<element.length; i++){
element[i][event]=action;
}}
}
onEvent(title, "onmouseover", editStyle(list, "display","list-item"));
//should set the title.onmouseover event to equal the function out put of editStyle
// which should change the list.style.display value to "list-item"