/    Sign up×
Community /Pin to ProfileBookmark

contents of form field

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>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@requestcodeMar 22.2003 — 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.
Copy linkTweet thisAlerts:
@michelleauthorMar 22.2003 — Thanks a million!

That saved me... ?

// Michelle
×

Success!

Help @michelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...