Page 1 of 1

Grab an element's outer <a> element

Posted: Thu Nov 30, 2006 6:13 pm
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

Posted: Thu Nov 30, 2006 6:33 pm
by nickvd
You answered your own question, next time try it first! ;)

Code: Select all

 document.getElementById('picture').parentNode.href = null

Posted: Thu Nov 30, 2006 6:46 pm
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:

Posted: Mon Dec 11, 2006 11:55 am
by neel_basu
It Would Be
===============

Code: Select all

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