Grab an element's outer <a> element

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Grab an element's outer <a> element

Post by Luke »

I have an image wrapped in an anchor tag...

Code: Select all

<a href="#url"><img src="#href" alt="Image" id="i_am_an_image"></a>
I have a reference to the image tag... how would I use that to grab the <a> tag that is it's parent?

Code: Select all

var img1 = document.getElementById('i_am_an_image');
//img1.parentNode ??
I want to disable the <a> tag's href but the only thing I have access to is the image
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

You answered your own question, next time try it first! ;)

Code: Select all

 document.getElementById('picture').parentNode.href = null
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

well I tried alert(img1.parentNode) and got "undefined" so I figured that wasn't it... but shortly after I posted here, I tried alert(img1.parentNode.href); and I got a url... sorry :oops:
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

It Would Be
===============

Code: Select all

document.getElementById('i_am_an_image').parentElement.href;
NOT parentNode
ITS parentElement
Post Reply