By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,970 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,970 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Aug 5 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ni**********@lexisnexis.com wrote:
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.h ref;
that said i may have gotten it wrong completely :p

--
Hope This Helped and MTFBWY...
Kieren aka JediFans - <URL:http://jedifans.com/>
The Force Is With Me, SuSE Linux Professional 9.3, Mozilla Firefox
1.0.6, Mozilla Thunderbird 1.5 Alpha 2 and Revenge Of The Sith!
Aug 5 '05 #2

P: n/a
ASM
ni**********@lexisnexis.com wrote:
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[i].src.substring(I[i].src.lastIndexOf('/')+1)
if(pict==thisImg) {
Link = I[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>
--
Stephane Moriaux et son [moins] vieux Mac
Aug 5 '05 #3

P: n/a
<ni**********@lexisnexis.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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'));">F ind
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>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Aug 5 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.