...

View Full Version : Onclick question



Typhoid Timmy
12-11-2006, 05:16 PM
My situation is that I want my script to:

a)detect if the field is empty (no problem)
b)if the field is empty to pull data from 2 other different fields and populate the empty one. (where I am having the problem)

Can an onclick populate an empty field with 2 others from its own form and not overwrite it if it was already filled? If so, can someone show me an example of the script? I searched a bit and was not able to find an answer.

nikkiH
12-11-2006, 09:23 PM
Sure it can. It can call any function you like, or you can do it inline.
I like functions.

function foo()
{
// your code here
}

<yourelement onclick="foo();">

How you set this all up depends on your form and all that jazz, so I can't show you too much without at least the html of your form.

Arbitrator
12-12-2006, 09:40 AM
Can an onclick populate an empty field with 2 others from its own form and not overwrite it if it was already filled?Iím assuming that youíre asking whether or not you can add the contents of field A to the empty field then not have those contents overwritten when having the contents of field B appended to that same (previously empty) field. If so, thatís easy enough:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="Author" content="Patrick Garies"/>
<title>XHTML 1 Strict Document</title>

<style type="text/css">
html { padding: 1em; }
body { margin: 0; }
fieldset { padding: 0.5em; text-align: center; }
</style>

<script type="text/javascript">
function combine() {
var input = document.getElementsByTagName("input");
if (!input[2].value) input[2].value = input[0].value + input[1].value;
else alert("The input field is not empty.\nUnable to comply.");
}
</script>

</head>
<body>

<fieldset>
<input type="text" value="basket"/>
<input type="text" value="ball"/>
<input type="text"/>
<input type="submit" value="Combine" onclick="combine();"/>
</fieldset>

</body>
</html>

Typhoid Timmy
12-12-2006, 11:39 PM
That was exactly what I was looking for....thanks Arb!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum