This is why JavaScript does my head in

Associate
Joined
30 Dec 2005
Posts
415
:mad:

An incredibly simple problem:
Code:
function checkforstring(fieldname) {
	alert(fieldname);
	var formfieldvalue = document.getElementById(fieldname).value
	alert(formfieldvalue);
}

When running that, it alerts the value of the parameter passed to the function (the first alert), then Firefox returns the following error:
Error: document.getElementById(fieldname) has no properties
Source File: http://localhost:8084/shop/index.php?edit
Line: 15

Could someone with a bit JavaScript brain help me out on this one.

Cheers,
Rich
 
Spunkey said:
to get the value property of a form field you need to use getElementsByName(fieldname)

PITA, yes, but it works.

It's a start! Trust me to get the function name wrong :p

A bit of modifying and I have the following working code:

Code:
function checkforstring(fieldname) {
	alert(fieldname);
	formfieldvalue = document.getElementsByName(fieldname)[0];
	alert(formfieldvalue.value);
}

Thanks very much :)
 
Last edited:
Back
Top Bottom