Luis wrote:
I have a function sililar to the one below on one of my pages. It puts
the value of a field on a form into a variable called FieldName:
function checkSomething( ) {
var FieldName = document.frmFor mName.FieldName .value
}
I'd like to reuse the function on a different page (instead of writing
another almost identical script on that page), but I need to change the
name of the field in the script depending on the page that the script is
used on.
How do I change the script so that I can put the script in an includes
file and then pass the name of the field to the script according to the
page that the script is being used?
You can pass a reference to something rather than hard code the
reference. Below some code that is fairly self explainatory.
You could also pass the name or id of something as a string, then use
something like:
function checkSomething( x) {
var theThing = document.getEle mentById(x);
....
... onclick="checkS omething('theFo rm');"....
Cheers, Fred,
It shows how to pass a reference to 'this',
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Re use Script</title>
<script type="text/javascript">
function checkSomething( a) {
var msg = 'You passed: ' + a;
if (a == '') msg += 'an empty string';
if (a.nodeName) msg += '\n Node name: ' +a.nodeName;
if (a.type) msg += '\n Type: ' + a.type;
if (a.id) msg += '\n ID: ' + a.id;
if (a.name) msg += '\n NAME: ' + a.name;
if (a.value) msg += '\n VALUE: ' + a.value;
alert(msg);
}
</script>
</head>
<body>
<div id="aDiv" onclick="checkS omething(this); "
style="padding: 10px 30px 10px 30px; background-color: #99cccc;
width: 200px; height 40px;">A clickable Div</div>
<form action="" name="theForm">
<input type="text" name="aTextInpu t" size="50"><br>
<input type="button" name="aButton" value="Pass the form"
onclick="checkS omething(this.f orm);">
<input type="button" name="aButton" value="Pass the 'text' input"
onclick="checkS omething(this.f orm.aTextInput) ;">
<input type="button" name="aButton"
value="Pass the 'text' input value"
onclick="checkS omething(this.f orm.aTextInput. value);">
<input type="reset" value="Reset">
</form>
</body>
</html>