/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Dynamic field in a function (simple)

I had a simple ajax function (note: this is not an ajax question) that was working fine:

[CODE]function ajaxFunction()

document.myForm.my_field_name.innerHTML = ajaxRequest.responseText;

ajaxRequest.open(“GET”, “/ajax/fieldlist.php?value=” + document.myForm.my_other_field_name.value, true);

[/CODE]

I want to make the fields dynamic, so I passed them in the function parameters, and now it isn’t working:

[CODE]function ajaxFunction(modifyfield,targetfield)

document.myForm.getElementById(targetfield).innerHTML = ajaxRequest.responseText;

ajaxRequest.open(“GET”, “/ajax/fieldlist.php?value=” + document.myForm.getElementById(modifyfield).value, true);

[/CODE]

Any help would be greatly appreciated. I’m very confused here.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyFeb 04.2009 — I have used this before and it works perfectly:
[CODE]var ele = document.forms['myFormName'].elements;
ajaxRequest.open("GET", "/ajax/fieldlist.php" +
"?" + encodeURI(ele['myFieldName'].name) +
"=" + encodeURI(ele['myFieldName'].value), true);[/CODE]

Naturally, if you already know what name you wish to use in the query string, then you don't have to go for the [B]name[/B] property. But, I wanted to show you how it can be done for when the field name is coming from a variable or when you are building a longer query string using a for loop.
Copy linkTweet thisAlerts:
@davidsakhauthorFeb 05.2009 — That works brilliantly! Thank you. ?
×

Success!

Help @davidsakh 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.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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