How to find out the anchor link href value of a given image?

N

ningjun.wang

Hello:

My html file contains the following image link:

<a href="some_url"><img src="MyImage.gif"></a>

How can I use Javascript to find out the value of some_url for the
given image name "MyImage.gif"?

I know how to get the image object with a given name (e.g.
"MyImage.gif"). But then how do I get the parent anchor link object and
find its href value?
 
J

Jedi Fans

Hello:

My html file contains the following image link:

<a href="some_url"><img src="MyImage.gif"></a>

How can I use Javascript to find out the value of some_url for the
given image name "MyImage.gif"?

I know how to get the image object with a given name (e.g.
"MyImage.gif"). But then how do I get the parent anchor link object and
find its href value?
this may or may not work: if the image has an id use this:
var it=document.getElementById('imageid').parentNode.href;
that said i may have gotten it wrong completely :p
 
A

ASM

Hello:

My html file contains the following image link:

<a href="some_url"><img src="MyImage.gif"></a>

How can I use Javascript to find out the value of some_url for the
given image name "MyImage.gif"?

I know how to get the image object with a given name (e.g.
"MyImage.gif"). But then how do I get the parent anchor link object and
find its href value?

document.images['myImage_1'].parentNode.href

file test.htm :
<html>
<script type="text/javascript"><!--
function linkOfImage(pict) {
var Href=null, Link, thisImg;
var I = document.getElementsByTagName('IMG');
for(var i=0;i<I.length;i++) {
thisImg = I.src.substring(I.src.lastIndexOf('/')+1)
if(pict==thisImg) {
Link = I.parentNode;
if(Link && Link.tagName.toLowerCase()=='a')
Href = Link.href;
}
}
if(Href) alert('link href = '+Href);
else alert('not found');
}
// --></script>
<p><a href="#" onclick="linkOfImage('i_2.jpg');">image href i_2.jpg</a>
<p><a href="t_1.htm"><img src="i_1.jpg"></a>
<p><a href="t_2.htm"><img src="i_2.jpg"></a>
<p><a href="t_3.htm"><img src="i_3.jpg"></a>
</html>
 
G

Grant Wagner

Hello:

My html file contains the following image link:

<a href="some_url"><img src="MyImage.gif"></a>

How can I use Javascript to find out the value of some_url for the
given image name "MyImage.gif"?

I know how to get the image object with a given name (e.g.
"MyImage.gif"). But then how do I get the parent anchor link object
and
find its href value?

<a href="http://www.yahoo.com"><img name="test" src="test.gif"></a>
<button onclick="alert(getParentHrefFromImage('test'));">Find
href</button>
<script type="text/javascript">
function getParentHrefFromImage(imageName)
{
if (document.images && document.images[imageName])
{
var href;
for (var el = document.images[imageName]; (el = el.parentNode);)
{
if ('string' == typeof el.tagName &&
'a' == el.tagName.toLowerCase())
{
return el.href;
}
}
}
return null;
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top