Reusing a script

L

Luis

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.frmFormName.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?
 
F

Fred Oz

Luis said:
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.frmFormName.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.getElementById(x);
....

... onclick="checkSomething('theForm');"....


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>Reuse 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="checkSomething(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="aTextInput" size="50"><br>
<input type="button" name="aButton" value="Pass the form"
onclick="checkSomething(this.form);">
<input type="button" name="aButton" value="Pass the 'text' input"
onclick="checkSomething(this.form.aTextInput);">
<input type="button" name="aButton"
value="Pass the 'text' input value"
onclick="checkSomething(this.form.aTextInput.value);">
<input type="reset" value="Reset">
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top