Click to See Complete Forum and Search --> : contents of form field


michelle
03-22-2003, 08:15 AM
I want to see the contents of a formfield by passing the formfield-name to a function. I just can't seem to get it working.

See code below...

function postThis(fieldName) {
alert(document.myForm.fieldName.value);
}

<form name="myForm" action="nextPage.htm">
<input type="text" name="firstname">
</form>
<a href="javascript:postThis('firstname');">show fistname</a>

requestcode
03-22-2003, 09:05 AM
Try this example:
<html>
<head>
<title>Check URL</title>
<script language="JavaScript">
function postThis(fieldname) {
alert(eval("document.myForm."+fieldname+".value"));
}
</script>
</head>
<body>
<form name="myForm">
<input type="text" name="firstname" size="20">
</form>
<a href="javascript:postThis('firstname');">show fistname</a>
</body>
</html>

Your problem was that the script was treating it as a value of 'firstname' rather than the field name. You can use the eval() method to create a JavaScript statement from that.

michelle
03-22-2003, 09:08 AM
Thanks a million!
That saved me... :D

// Michelle