Is there a way to get the parent object of a function? Like which object activated the function?
Here’s the JS
var ua=false
function toggleUA() {
if (ua==false)
{document.getElementById("ua").src="http://i805.photobucket.com/albums/yy331/NatFan/uamap.png";ua=true;}
else
{document.getElementById("ua").src="http://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png";ua=false;} }
Here’s the HTML
<img class="airline" id="ua" src="http://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png" width="1500" height="744">
<table class="nav">
<tr>
<td class="star" onclick='toggleUA();'>United Airlines</td>
</tr>
</table>
So If I wanted to have the table cell change colors when i click it, how would I do that?
I think the safer way is to pass
thisto the function call as the first argument, and then use that to manipulate it. Such as:And then the Javascript function would be declared as:
Where
objis the<td>that was clicked (in case you use this on several<td>elements).Now, normally the way event listeners works is that you can usually reference
thisin the function automatically without having to pass it as an argument, but the way inline event handlers works isn’t the same, and you need to passthis.UPDATE:
To modify the background color or other style properties, you can use the
.styleproperty of the element. With this, you should be able to modify any CSS property, and for background color, use:https://developer.mozilla.org/en-US/docs/DOM/element.style