/    Sign up×
Community /Pin to ProfileBookmark

Absolute addressing of Form.Field

Having a very huge application, I have lots of forms (with absolutely unique IDs) and many Field-Ids that are [COLOR=”Red”]NOT[/COLOR] unique. For example the field “project_id” appears in about 4 different forms:

[FONT=”Courier New”][SIZE=”2″]
[COLOR=”Navy”]<form id=’formaaa’>
<input id=’project_id’ … />

</form>
<form id=’formbbb’>
<input id=’project_id’ … />

</form>[/COLOR]
[/SIZE]
[/FONT]

It is not possible to make the field-ids unique!!

Prototype’s $F() function can’t handle this.

I am searching for an absolute reference, that works in Mozilla too. And I want to avoid “name=” for Form and Field!

[FONT=”Courier New”][SIZE=”2″][COLOR=”Red”]function putfieldvalue(pForm,pField,pValue) {
????? = pValue;
}[/COLOR]
[/SIZE]
[/FONT]
for ????? I tried all of the following (all don’t work):
[FONT=”Courier New”][SIZE=”2″]document.getElementByid(pForm).getElementByid(pField).value[/SIZE][/FONT]
— or —
[FONT=”Courier New”][SIZE=”2″]wform = document.getElementByid(pForm);
wform.pField.value =[/SIZE]
[/FONT]
— or —
[FONT=”Courier New”][SIZE=”2″]wform = document.getElementByid(pForm);
wfield = document.getElementByid(pField);
wform.wfield.value =[/SIZE]
[/FONT]
— and many others.

Can anybody help me? I’m really stuck.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 15.2009 — loop through the elements in the form:var aObj=document.getElementById('formbbb').getElementsByTagName('input');
for(var i=0; i&lt;aObj.length; i++) {
if(aObj[i].id=='project_id') {alert(aObj[i].value);};
break;
}
Copy linkTweet thisAlerts:
@ikedixauthorMar 20.2009 — Thanks a lot Fang!

I made two functions, to ease up:

// put form value

function pfv (pForm,pField,pValue) {

if (!$(pForm)) {alert('Form does not exist (pfv, parameter 1): '+pForm); return;};

if (!document.getElementById(pField)) {alert('Field does not exist (pfv, parameter 2): '+pField); return;};

var okay=0;

var aObj=document.getElementById(pForm).getElementsByTagName('input');

for (var i=0; i<aObj.length; i++) {

if(aObj[i].id==pField) {

aObj[i].value = pValue;

okay="1";

break;

} else {

//alert("pfv i="+i+" id="+aObj[i].id);

};

};

if (okay==0) alert("pfv has no hits! length="+aObj.length+" pForm="+pForm+" pField="+pField+" pValue="+pValue);

}



// get form value

function gfv (pForm,pField) {

if (!$(pForm)) {alert('Form does not exist (pfv, parameter 1): '+pForm); return;};

if (!document.getElementById(pField)) {alert('Field does not exist (pfv, parameter 2): '+pField); return;};

var okay=0;

var aObj=document.getElementById(pForm).getElementsByTagName('input');

for (var i=0; i<aObj.length; i++) {

if(aObj[i].id==pField) {

wvalue = aObj[i].value;

okay="1";

break;

} else {

//alert("pfv i="+i+" id="+aObj[i].id);

};

};

if (okay==0) alert("gfv has no hits! length="+aObj.length+" pForm="+pForm+" pField="+pField);

return wvalue;

}
Copy linkTweet thisAlerts:
@toicontienMar 20.2009 — The browser takes great pains to create form field DOM node references for you.

[B]JavaScript, DOM, and the Humble FORM[/B]
×

Success!

Help @ikedix 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 6.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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