473,503 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
3 11595
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1608
by: Anna | last post by:
Hello all. I hope this is the right group to ask my question. I have an HTML page that contains an SVG file in an object. Inside an SVG file I want to add a link to link to some anchor on the...
4
3434
by: deko | last post by:
I use named anchors to take users to specific parts of a long page. But I want to add some processing and do some things with my nav bar when users go to certain sections delineated by named...
5
25836
by: elsenraat_76 | last post by:
Hello! I was wondering if someone could help me out with a problem I'm having? I'm trying to input a javascript value into an anchor tag (from a function), but don't have an event to call the...
8
4782
by: knoxautoguy | last post by:
This problem has consumed a lot of my time, and I'm aftraid someone will tell me that my whole approach to this objective is wrong; however, I would like to know if there is a way to do this. I...
16
4989
by: Lastwebpage | last post by:
Hello, I am little surprised about the following: <a href="#" ... I found some lines about the href tag for a site should point to an name or in XHTML to an ID, but in both cases this seems to...
21
9096
by: Prisoner at War | last post by:
Hi, People, Is it possible to have an "empty" or "dummy" <a href***without*** the browser jumping back up the page?? I have a hyperlink that doesn't point to another document, but is used to...
1
7725
by: praveenb000 | last post by:
Hi every one, I designed a web page, having horizontal menu using UL and LI tags; I need to be set rollover effect for a menu items. whenever user hover on a menu item, the entire...
2
2061
by: everly | last post by:
Hi, I'm helping my friends band and making their MySpace. I messed something up and I can't figure out what it is. Everything is all out of whack on the bottom half of the page. The comments...
10
40575
by: bgold12 | last post by:
Hey, I know of two ways to place a link on an HTML page; (1) use the anchor element, and (2) use javascript to respond to an event (such as onclick) and set the location. I recently started...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.