View Full Version : javascript selection into form
davidmurphy
08-26-2002, 07:41 PM
Hi,
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
David
beetle
08-26-2002, 08: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....<script>
function showText(text) {
document.forms[0].elements['output'].value = text;
}
</script>
(...HTML...}
<img src="map1.gif" alt="europe" onClick="showText(this.alt);" />
<form>
<input type="text" name="output" id="output" />
</form>
adios
08-27-2002, 12:51 AM
A flexible approach:
<html>
<head>
<title>untitled</title>
<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;
</script>
</head>
<body>
<img name="img1" src="http://www.codingforums.com/images/sendpm.gif"
onload="this.onmouseup=showImageText;">
<img name="img2" src="http://www.codingforums.com/images/find.gif"
onload="this.onmouseup=showImageText;">
<img name="img3" src="http://www.codingforums.com/images/buddy.gif"
onload="this.onmouseup=showImageText;">
<form name="f1">
<input type="text" name="t1">
</form>
</body>
</html>
The onmouseup is strictly for NS4 compatibility, which doesn't support onclick for images. Alter to suit.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.