Hello,

I'm wondering how to add predefined text to many input fields on click of an image.

so I have 4 images and I click one and 4 fields are filled with predefined text. If I click another the text is changed with some other predefined text.

Thanks

Recommended Answers

All 4 Replies

I think this may help.

<html>
<head>
<script type="application/javascript">
	function msg() {
		for(var x=1;x<=4;x++) {
			document.getElementById('a' + x).value="hello";
		}
	}
</script>
</head>
<body>
	<img src="" height="50" width="200" onClick="javascript:msg();"><br />
	<input type="text" name="a1" id="a1">
    <input type="text" name="a2" id="a2">
    <input type="text" name="a3" id="a3">
    <input type="text" name="a4" id="a4">
</body>

SNIP

Thanks. How do I add different text to the fields? Say if it's a name, address, postal code,school.

try this.

<html>
<head>
<script type="application/javascript">
	function msg() {
		document.getElementById('a1').value="name";
		document.getElementById('a2').value="address";
		document.getElementById('a3').value="postal";
		document.getElementById('a4').value="school";
	}
</script>
</head>
<body>
	<img src="" height="50" width="200" onClick="javascript:msg();"><br />
	<input type="text" name="a1" id="a1">
    <input type="text" name="a2" id="a2">
    <input type="text" name="a3" id="a3">
    <input type="text" name="a4" id="a4">
</body>

SNIP

commented: Thanks, again +1

Lol. Thanks very much. Both work well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.