View Full Version : javascript selection into form

08-26-2002, 08:41 PM

I am a newbie of Javascript so please excuse the question.

I am looking for a javascript that when you click on an image the selection that you clicked on will appear in a text box forum.

eg. if you click on 'map1.gif' the word 'europe' will appear in a text box below.

I would greatly appreciate it if anyone could point me in the right direction.

Kind Regards


08-26-2002, 09:05 PM
Well, like many things, there are several ways to approach this. The easiest way would be to utilize the 'alt' attribute of the image. Like this....
function showText(text) {
document.forms[0].elements['output'].value = text;


<img src="map1.gif" alt="europe" onClick="showText(this.alt);" />

<input type="text" name="output" id="output" />

08-27-2002, 01:51 AM
A flexible approach:

<script type="text/javascript" language="javascript">

var Imagetext = Object;
Imagetext['img1'] = 'Send Private Message'; //onclick text, indexed by image name
Imagetext['img2'] = 'Do a Search';
Imagetext['img3'] = 'Add to Buddy List';

function setImageText() {
for (img in Imagetext) document.images[img].text = Imagetext[img];

function showImageText() {
document.f1.t1.value = this.text;

onload = setImageText;

<img name="img1" src="http://www.codingforums.com/images/sendpm.gif"
<img name="img2" src="http://www.codingforums.com/images/find.gif"
<img name="img3" src="http://www.codingforums.com/images/buddy.gif"
<form name="f1">
<input type="text" name="t1">

The onmouseup is strictly for NS4 compatibility, which doesn't support onclick for images. Alter to suit.