In my code, I have set an eventHandler on the document itself that targets a specific element with an id. This targeted element is a div containing some content. The eventHandler is triggered by a 'click' action on the document. However, I have encountered an issue where clicking on elements inside the div does not trigger the eventHandler. I need to click on the edge of the div for the eventHandler to work correctly.
Is there a solution to this problem? I considered adding an a-element with height/width = 100%, but I'm unsure how to make it execute a JavaScript function rather than redirecting to a link.
Here's a snippet of the relevant code:
var inner = createDiv(['page-content', 'small-page-content', 'hover']);
document.addEventListener('click', function (e) {
if (hasId(e.target, index.toString())) {
makeLarge(index);
}
}, false);
function hasId(elem, id) {
return elem.id == id;
}
function createDiv(classes=[]) {
var div = document.createElement('div');
for (var i = 0; i<classes.length; i++) {
div.classList.add(classes[i]);
}
return div;
}
The value of index.toSring() is used as the id for the div. This particular div contains an image element, an h1 element, and two sub-divs each having an h3 element and an ol list. My goal is to make the entire div clickable. Currently, only the edges are clickable while elements like images are not responsive to clicks.
For more context, please refer to this JSFiddle example. In the provided example, you can see that the text within the div is not clickable, but the edges of the div respond to clicks (where the div is uncovered by other elements).
Please provide solutions using plain JavaScript without jQuery.