'Javascript : Why can't I use the "this" with childnodes?
<div id="test" onmouseover="working(this)">
<p>help</p>
<p>me</p>
</div>
<div id="test" onmouseover="not_working(this)">
<p>help</p>
<p>me</p>
</div>
When I use the first function(not_working) it doesn't work, but when I used the second function(working) it does.
function not_working(element){
document.getElementById(element).childNodes[1].innerHTML="succeed";}
function working(element){
document.getElementById("test").childNodes[1].innerHTML="succeed";}
Sorry if I miss something simple, I'm still learning html and only have limited knowledge on javascript.
Solution 1:[1]
this is a reference to the element, the event was created from (the div in your case), but getElementById takes an id ("test" in your case).
Also the hovered element is already being passed in the function as element so you can just do:
function working(element) {
element.childNodes[1].innerHTML="succeed"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Timm Nicolaizik |
