Forum Moderators: open

Message Too Old, No Replies

Getting a value in JavaScript when am image is clicked

Need to get value of image clicked to pass back to the parent.page

         

STUCKinSPAIN

7:18 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



I am suffering old age I think!

I am trying to get the value [filename] of an image that is clicked in order to pass it back to the parent page which opened the popup displaying all available images. Because I am using FSO, I will need to use the filename+extension [pic.jpg] as the value.

This is what I have so-far:
[you will note that I have hard-coded the filename in - it works this way - I just need to grab that info from the clicked image rather than running the script multiple times to do the same thing]

<title>Pick an image file ... </title>
<SCRIPT language="Javascript">
function pick_image()
{
if (window.opener &&!window.opener.closed)
{
window.opener.document.<% =request("pageupdate_form") %>.<% =request("image_field") %>.value ="meal.jpg"
}
window.close();
}
</SCRIPT>
</head>
<body link="#663300" vlink="#663300" alink="#663300" topmargin="2" leftmargin="2" rightmargin="2" bottommargin="2">
<table border="0" cellpadding="20" style="border-collapse: collapse" id="table3">
<tr>
<td align="center"><b><a href="javascript:pick_image('meal.jpg')"><img name="imageLoc" src="../images/meal.jpg" width="70"><br><font size="1" face="Verdana">Meal.jpg</font></a></b>
<td align="center"><b><a href="javascript:pick_image('meala.jpg')"><img name="imageLoc" src="../images/meala.jpg" width="70"><br><font size="1" face="Verdana">Meala.jpg</font></a></b>
<td align="center"><b><a href="javascript:pick_image('mealb.jpg')"><img name="imageLoc" src="../images/mealb.jpg" width="70"><br><font size="1" face="Verdana">Mealb.jpg</font></a></b>
<td align="center"><b><a href="javascript:pick_image('mealc.jpg')"><img name="imageLoc" src="../images/mealc.jpg" width="70"><br><font size="1" face="Verdana">Mealc.jpg</font></a></b>
</td>
</tr>
</table>

Regards
Glen

ericjust

7:30 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



You mean something like this?

<SCRIPT language="Javascript">
function pick_image(img_src)
{
if (window.opener &&!window.opener.closed)
{
window.opener.document.<% =request("pageupdate_form") %>.<% =request("image_field") %>.value = img_src;
}
window.close();
}
</SCRIPT>

Fotiman

8:16 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's how I would do it (or at least, how I would begin to change your example... I'd likely get rid of the tables as well). Note, I've altered your example to contain semantic content, to validate, and to separate content, presentation, and behavior.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
body { margin: 2px; }
a { color: #630; display: block; }
table { border: 0; border-collapse: collapse; }
td { padding: 20px; text-align: center; font-weight: 700; font-size: 60%; font-family: Verdana,sans-serif; }
img { width: 70px; display: block; }
</style>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>Pick an image file ... </title>
<script type="text/javascript">
function init()
{
// Attach event handlers
var imgLinks = document.getElementsByTagName('a');
for( var i = 0; i < imgLinks.length; i++ )
{
imgLinks[i].onclick = pick_image;
}
}
function pick_image()
{
var filestart = this.href.lastIndexOf("/");
filestart = (filestart == -1? 0 : filestart + 1);
var filename = this.href.substr(filestart);

return false;
if (window.opener &&!window.opener.closed)
{
window.opener.document.<% =request("pageupdate_form") %>.<% =request("image_field") %>.value = filename;
}
window.close();
return false;
}
window.onload = init;
</script>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<a href="../images/meal.jpg" ><img src="../images/meal.jpg" alt="Meal">Meal.jpg</a>
</td>
<td>
<a href="../images/meala.jpg"><img src="../images/meala.jpg" alt="Meala">Meala.jpg</a>
</td>
<td>
<a href="../images/mealb.jpg"><img src="../images/mealb.jpg" alt="Mealb">Mealb.jpg</a>
</td>
<td>
<a href="../images/mealc.jpg"><img src="../images/mealc.jpg" alt="Mealc">Mealc.jpg</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>