"Steve van Dongen" <st*****@hotmail.com> wrote in message
news:00********************************@4ax.com...
On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com> wrote:
I call a function in my .js file like this:
onClick="location.href='blank.html' +
generateSearchStringFromForm('section')"
where section is the name of my form.
The function is defined as follows:
myFunction(k) {
for (var i=0; i < document.forms[k].elements.length; i++) {
element = document.forms[k].elements[i];
//do stuff with each element
}
}
The problem is I'm getting the following error -
Error: Object expected
Code: 0
Surely the above code is OK?
I seem to get this error alot, even though it appears to work sometimes
withno complaint.
If there is an error in the code of another function in the same .js page
would this cause the call to this particular function to return such an
error?
Usually when you get Object Expected on line 0 it means you are
calling a function in the event handler but your script cannot be
parsed because of a syntax error.
It's unlikely you've posted enough code for anyone to help you, but
what you have posted has one obvious syntax error -- function
definitions start with the "function" keyword.
OK, Here is the whole function.
//set k to FORM NAME or INDEX to extract its field vlaues
function generateSearchStringFromForm(k) {
var element;
var srchStr = "?";
var last = document.forms[k].elements.length - 1;
for (var i=0; i < document.forms[k].elements.length; i++) {
element = document.forms[k].elements[i];
if (element.type == "text" || element.type == "textarea" ||
element.type == "hidden") {
nameValue = escape(element.name) + "=" + escape(element.value);
}
else if (element.type.indexOf("select") != -1) {
nameValue = escape(element.name) + "=" +
escape(element.options[element.selectedIndex].value);
}
else if (element.type == "checkbox" || element.type == "radio") {
if (element.checked) {
nameValue = escape(element.name) + "=" +
escape(element.value);
} else continue
} else continue
srchStr += nameValue;
//only add a '&' if there are more parameters
if (i < last)
srchStr += "&";
}
return srchStr;
}