Page 1 of 1

Alert parent element's ID?

Posted: Sat May 03, 2008 11:45 am
by JAB Creations
In JavaScript how can we dynamically alert an element's parent element's ID? (without using a library such as jQuery) :P Also by dynamic I mean without specifying any ID to begin with (I need to figure this out to be used in a JavaScript for loop)

<div id="i_want_to_alert_this_id">
<a onclick="alert(the_above());"...

Re: Alert parent element's ID?

Posted: Sat May 03, 2008 1:34 pm
by arjan.top
this.parentNode to get the parent, then get the id ...

Re: Alert parent element's ID?

Posted: Sat May 03, 2008 1:50 pm
by JAB Creations
Woot, thanks! :mrgreen:

Code: Select all

<div id="i_want_to_alert_this_id">  <a href="#" onclick="alert(this.parentNode.id)">alert parent ID</a> </div>

Re: Alert parent element's ID?

Posted: Sat May 03, 2008 1:52 pm
by Zoxive
I strongly suggest when playing around with javascript(AND Css) to use firebug.

The Inspect DOM is very powerful.

Also if you are going to use a function, when you call it you will need to give it a reference to that element by using this.

Code: Select all

<div id="i_want_to_alert_this_id">
<a onclick="the_above(this);"...