Alert parent element's ID?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Alert parent element's ID?

Post 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());"...
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Alert parent element's ID?

Post by arjan.top »

this.parentNode to get the parent, then get the id ...
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Alert parent element's ID?

Post 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>
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Alert parent element's ID?

Post 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);"...
 
 
Post Reply