By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

function w/ parameter

P: n/a
I have a function that when uses the form name works correctly. Should I
pass a variable to the function using either BizReset('bizform') or
BizReset(getElementById('bizform') statements I receive the error
"elements.length is either null or an object".
Any Suggestions?

<script = "text/javascript>
function BizReset(curform) {
len = curform.elements.length;
var blank = ""
for(i=0;i<len;i++) {
if (curform.elements[i].type == "text") {
curform.elements[i].value = blank;
curform.elements[i].disabled=false;
}
}
QuoteLayerOn(0);
DisplayButtons(0);
document.curform.BizName.focus();
}
</script>
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
danny wrote:
I have a function that when uses the form name works correctly. Should I
pass a variable to the function using either BizReset('bizform') or
Please hang in there, there's quite a bit to talk about here...

This method of calling your function is incompatible with the
code you have posted. If you pass the name of the form as a
string (as above), use:

function BizReset(curform) {
var f = document.forms[curform];
// now do things with f
BizReset(getElementById('bizform') statements I receive the error
If you are going to reference your form this way, you must put an
id on the form 'bizform' and fix the syntax error.

<form id="bizform" ... >
...

... onclick="BizReset(getElementById('bizform'));" ...
...

function BizReset(curform) {
// curform will be a reference to the form
var len = curform.elements.length;
...
"elements.length is either null or an object".
Any Suggestions?

<script = "text/javascript>
<script type="text/javascript">
function BizReset(curform) {
len = curform.elements.length;
Unless 'len' needs to be global, keep it local:

var len = curform.elements.length;
var blank = ""
for(i=0;i<len;i++) {
Same with 'i'

for(var i=0; i<len; i++) {

if (curform.elements[i].type == "text") {
curform.elements[i].value = blank;
The variable 'blank' is not needed, you could just write:

curform.elements[i].value = '';
curform.elements[i].disabled=false;
}
}
QuoteLayerOn(0);
DisplayButtons(0);
If these are not part of your problem, remove them for the sake
if fixing your error.
document.curform.BizName.focus();
You should check that the focus method is supported first (e.g.
older versions of Safari don't). And curform is a reference to
the form, so 'document.' will cause an error:

if (curform.BizName.focus) curform.BizName.focus();
}
</script>


Here is a modified version of your form that shows different ways
to do what I think you are trying to do.

<script type="text/javascript">
// Pass a reference to the form
function BizResetA(curform) {
var len = curform.elements.length;
for(var i=0;i<len;i++) {
if (curform.elements[i].type == "text") {
curform.elements[i].value = '';
curform.elements[i].disabled=false;
}
}
if (curform.BizName.focus) curform.BizName.focus();
}

// Pass the name of the form as a string
function BizResetB(curform) {
var f = document.forms[curform];
var len = f.elements.length;

for(var i=0;i<len;i++) {
if (f.elements[i].type == "text") {
f.elements[i].value = '';
f.elements[i].disabled = false;
}
}
if (f.BizName.focus) f.BizName.focus();
}

</script>
<form action="" name="BizForm">
<input type="text" name="BizName" value="something" disabled>
<input type="button" onclick="
BizResetA(this.form);" value="this.form">
<input type="button" onclick="
BizResetB('BizForm');" value="'BizName'">
<input type="reset">
</form>
--
Rob
Jul 23 '05 #2

P: n/a
danny wrote:
I have a function that when uses the form name works correctly. Should I
pass a variable to the function using either BizReset('bizform') or
BizReset(getElementById('bizform') statements I receive the error
"elements.length is either null or an object".
Any Suggestions?

BizReset(document.getElementById('bizform'));
or better:
BizReset(document.forms['bizform']);

Mick

<script = "text/javascript>
function BizReset(curform) {
len = curform.elements.length;
var blank = ""
for(i=0;i<len;i++) {
if (curform.elements[i].type == "text") {
curform.elements[i].value = blank;
curform.elements[i].disabled=false;
}
}
QuoteLayerOn(0);
DisplayButtons(0);
document.curform.BizName.focus();
}
</script>

Jul 23 '05 #3

P: n/a
Mick / Rob,

Thanks. Excellent explanations. Everything is working correctly.

"danny" <dt*****@planet-inc.net> wrote in message
news:11*************@corp.supernews.com...
I have a function that when uses the form name works correctly. Should I
pass a variable to the function using either BizReset('bizform') or
BizReset(getElementById('bizform') statements I receive the error
"elements.length is either null or an object".
Any Suggestions?

<script = "text/javascript>
function BizReset(curform) {
len = curform.elements.length;
var blank = ""
for(i=0;i<len;i++) {
if (curform.elements[i].type == "text") {
curform.elements[i].value = blank;
curform.elements[i].disabled=false;
}
}
QuoteLayerOn(0);
DisplayButtons(0);
document.curform.BizName.focus();
}
</script>

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.