473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

get image filename fom div

I've have a number of DIV tags that contain one image. Each DIV has an
onclick event. How can I get the image filename? I just need the
filename and not the file path.

I was trying to do something with getElementsByTagName but I'm stuck
trying to work out which one has as its parent DIV the DIV that was clicked.

Andrew Poulos
Jul 23 '05 #1
8 9173
Andrew Poulos wrote on 10 jul 2005 in comp.lang.javascript:
I've have a number of DIV tags that contain one image. Each DIV has an
onclick event. How can I get the image filename? I just need the
filename and not the file path.


<div onclick='blah'><img src="a.jpg"></div>
<div>other things</div>
<div onclick='blah'>some text <img src="b.jpg"></div>
<div onclick='blah'><img src="c.jpg"></div>
<br><br>
<div id='r'></div>

<script type="text/javascript">

var r = document.getElementById('r');

var a = document.getElementsByTagName("div");
for (var i = 0; i < a.length; i++) {
var b = a[i].getElementsByTagName("img");
if(b.length>0)
r.innerHTML+= 'divnr ' + (i+1) + ": " +
b[0].src.replace(/.*\//,'')+'<br>';
}

</script>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
Evertjan. wrote on 10 jul 2005 in comp.lang.javascript:
Andrew Poulos wrote on 10 jul 2005 in comp.lang.javascript:
I've have a number of DIV tags that contain one image. Each DIV has an
onclick event. How can I get the image filename? I just need the
filename and not the file path.


<div onclick='blah'><img src="a.jpg"></div>
<div>other things</div>
<div onclick='blah'>some text <img src="b.jpg"></div>
<div onclick='blah'><img src="c.jpg"></div>
<br><br>
<div id='r'></div>

<script type="text/javascript">

var r = document.getElementById('r');

var a = document.getElementsByTagName("div");
for (var i = 0; i < a.length; i++) {
var b = a[i].getElementsByTagName("img");
if(b.length>0)
r.innerHTML+= 'divnr ' + (i+1) + ": " +
b[0].src.replace(/.*\//,'')+'<br>';
}

</script>


reading your question again, perhaps this is what you want?

<div onclick='blah(this)'><img src="a.jpg"></div>
<div>other things</div>
<div onclick='blah(this)'>some text <img src="b.jpg"></div>
<div onclick='blah(this)'><img src="c.jpg"></div>
<script type="text/javascript">

function blah(x){
var b = x.getElementsByTagName("img");
if(b.length>0)
alert(b[0].src.replace(/.*\//,''))
}

</script>
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3
ASM
Andrew Poulos wrote:
I've have a number of DIV tags that contain one image. Each DIV has an
onclick event. How can I get the image filename? I just need the
filename and not the file path.


<script type="text/javascript">
<!--
function getImgFileName(thisDiv) {
var imgPath = thisDiv.getElementsByTagName('img')[0].src;
var imgFile = imgPath.substring(imgPath.lastIndexOf('/')+1,imgPath.length);
alert('img file = '+imgFile);
}
//-->
</script>
<p onclick="getImgFileName(this)"><img src='photo1.jpg'>photo 1</p>

<div onclick="getImgFileName(this)">
<h2>photo 2</h2>
<img src='TE_Prefs.jpg'>
<p>few words of description
</div>

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #4
Evertjan. wrote:
Evertjan. wrote on 10 jul 2005 in comp.lang.javascript:

Andrew Poulos wrote on 10 jul 2005 in comp.lang.javascript:

I've have a number of DIV tags that contain one image. Each DIV has an
onclick event. How can I get the image filename? I just need the
filename and not the file path.


<div onclick='blah'><img src="a.jpg"></div>
<div>other things</div>
<div onclick='blah'>some text <img src="b.jpg"></div>
<div onclick='blah'><img src="c.jpg"></div>
<br><br>
<div id='r'></div>

<script type="text/javascript">

var r = document.getElementById('r');

var a = document.getElementsByTagName("div");
for (var i = 0; i < a.length; i++) {
var b = a[i].getElementsByTagName("img");
if(b.length>0)
r.innerHTML+= 'divnr ' + (i+1) + ": " +
b[0].src.replace(/.*\//,'')+'<br>';
}

</script>


reading your question again, perhaps this is what you want?

<div onclick='blah(this)'><img src="a.jpg"></div>
<div>other things</div>
<div onclick='blah(this)'>some text <img src="b.jpg"></div>
<div onclick='blah(this)'><img src="c.jpg"></div>
<script type="text/javascript">

function blah(x){
var b = x.getElementsByTagName("img");
if(b.length>0)
alert(b[0].src.replace(/.*\//,''))
}

</script>


Thanks for the help (and to ASM as well). I never realised that you
could do getElement... with an object other than document. It's going to
change some of my other code for the better.

thanks again
Andrew Poulos
Jul 23 '05 #5
ASM
Andrew Poulos wrote:

Thanks for the help (and to ASM as well). I never realised that you
could do getElement... with an object other than document


you can have a look to that :
http://perso.wanadoo.fr/stephane.mor...OnClick_en.htm
that allow you to do not have hard html codding the 'onclick'

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #6
ASM wrote:
Andrew Poulos wrote:

Thanks for the help (and to ASM as well). I never realised that you
could do getElement... with an object other than document

you can have a look to that :
http://perso.wanadoo.fr/stephane.mor...OnClick_en.htm
that allow you to do not have hard html codding the 'onclick'


Hmm, that's pretty interesting stuff. Where do I find out more about
using the DOM?

Andrew Poulos
Jul 23 '05 #7
ASM
Andrew Poulos wrote:
ASM wrote:
http://perso.wanadoo.fr/stephane.mor...OnClick_en.htm
that allow you to do not have hard html codding the 'onclick'

Hmm, that's pretty interesting stuff. Where do I find out more about
using the DOM?


My refs are prerably in french ... but :

a good approach whith examples :
http://en.selfhtml.org/javascript/index.htm
curiously I'd gotten it in german ! ?

very serious :
http://www.mozilla.org/docs/dom/domr..._shortTOC.html

Try in google : cloneNode
or parentNode or appendChild

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #8
ASM
Andrew Poulos wrote:
ASM wrote:
you can have a look to that :
http://perso.wanadoo.fr/stephane.mor...OnClick_en.htm
that allow you to do not have hard html codding the 'onclick'

Hmm, that's pretty interesting stuff. Where do I find out more about
using the DOM?


in same idea (pages in fr):
http://perso.wanadoo.fr/stephane.mor...t_cells_fr.htm
http://perso.wanadoo.fr/stephane.mor...ht_rows_fr.htm

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
by: tjv | last post by:
Hi all, I am working with images in python using PIL. I come from a MATLAB background so I am finding it to be sometimes frustrating to do things correctly and quickly. All I need to do is load an...
17
by: PyPK | last post by:
Hi I am looking for a simple tiff Image reader/writer in python.Can anyone point me to the right one.
8
by: Wally | last post by:
I would like to have an image with a caption displayed below it. The size of the image will vary. The caption should not extend beyond the width of the image. How can I cause the text of the...
4
by: jens Jensen | last post by:
Hello, I have an image file name in a table that a let users modify via a GridView/DetailView pair. * The actual image file is saved in the image folder of the application. But the image is...
0
by: bananularstyle | last post by:
Hi everyone, This is an issue I have been picking away at for the past two weeks and am running short of ideas. :-\ I am writing an ASP page that allows a user to open/save a tif image stored on...
0
by: nma | last post by:
Hiya, Can anyone help me in this..i'm stuck I have created a table in SQL server that contain field name (shotSelectedKeyfrm -int type, and imgSelectedKeyfrm-varchar(255) type) like below: ...
2
by: mac | last post by:
Hi, I'm looking for a script that will allow me to upload images to a folder and shows pagination... pls can any body help me? i hav done with the uploading, but not able to do with the...
14
anfetienne
by: anfetienne | last post by:
hi.....i have this script (below #1) that is linked to another php file SimpleImage.php (#2) im trying to get it to work on my uploaded images but it keeps coming up errors.....i haven't altered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.