I need to get the parentNode of a clicked element in plain JS (no jQuery or other frameworks)
I am currently using document.getElementById(\"item_click\")
but
function new_class(event) {
wTile = event.target.parentNode;
wTile.className = wTile.className + " added-class";
}
"I just don't know how to integrate
this
in the script"
Use .call()
to invoke the handler to set its this
value to the element that has the handler...
<div id="item_click" onmousedown="new_class.call(this,event)" ...>
function new_class(event) {
var wTile = this.parentNode;
wTile.className = wTile.className + " added-class";
}
You can just use event.target.parentNode
(Don't need to even pass the event
in the function):
function new_class() {
var parent = event.target.parentNode;
// Do whatever...
}