/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Using a variable to reference a form name

Hello,

I have a feeling this is a simple question, but I just can’t get it to work. I have a page with many forms updatenotes_1137″, “updatenotes_1138”, “updatenotes_1139”, and so on. Each of these forms have a text input named “b_user.” I want to call a JS function with the name of the form and have it change the value of “b_user” from that field.

Here’s a summary of my attempt (this is the exact code I’m working with while trying to figure it out):

[CODE]<html>
<head>
<script>
function add_B_User(form_Name)
{
//document.[“form_Name”].b_user.value = “Hello World!”;
alert(form_Name);
}
</script>
</head>

<body>

<form id=’updatenotes_1139′ action=index.php method=POST>
<input size=20 type=input name=’b_user’ value=”LVS BOOKS”>
<input type=submit>
</form>

<script>
add_B_User(“updatenotes_1139”);
</script>

</body>
</html>[/CODE]

Now, if i only use the alert:

[CODE]function add_B_User(form_Name)
{
//document.[“form_Name”].b_user.value = “Hello World!”;
alert(form_Name);
}[/CODE]

The alert works just fine, transmitting the input name. So it’s gotta be something in the syntax of that the “Hello world!” line of code. Just not sure what ?.

Thanks for any help!
– Alex

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Alex_909authorJun 04.2013 — As a side note, the function also works when I hardcode in the form name:
[CODE]function add_B_User(form_Name)
{
document.forms.updatenotes_1139.b_user.value = "Hello World!";
}
</script>[/CODE]


Thanks!
Copy linkTweet thisAlerts:
@Alex_909authorJun 04.2013 — Facepalm. I'm not used to Javascript, but even I should have realized after this many hours that I was using id-based functions while only labeling the actual inputs with "name" and not "id." Ugh. Well, thanks anyhow all ?.
×

Success!

Help @Alex_909 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.4,
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,
)...