I have the following function:
function toggleContent()
{
var parent = this.parentElement;
toggleClass(parent,"detailsAreVisible");
}
This function is being called when a user clicks on a certain button. The binding happens in plain javascript like this:
var allTogglingButtons = document.querySelectorAll("[unhideicon],[hideicon]");
crossBrowserAddClickEvent(allTogglingButtons, toggleContent);
The crossBrowserAddClickEvent function is necessary to fix another IE problem:
function crossBrowserAddClickEvent(array, functionName)
{
for(var i=0; i < array.length; i++)
{
if (array[i].addEventListener)
{
aray[i].addEventListener('click', functionName);
}
else if (array[i].attachEvent)
{
array[i].attachEvent('onclick', functionName);
}
}
}
This works fine in chrome and firefox, because the "this" variable is set to the button I'm clicking on inside the toggleContent function. However, in IE "this" is equal to the (default) global window / document object and of course calling .parentElement on that yields null. Why is "this" not the clicked button?
For reference, the "button" i'm clickingon is actually an svg element:
<svg unhideicon class="svgIcon"> .... </svg>