/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Error with parsing

This code function is to put values in a form which it gets from cookies.
I know it is far form perfect, but it shoud work.
the argument formName is the name of the form.

[code=php]
function returnFormCookies(formName){
var elem = document.getElementById(formName).elements; for (var i = 0; i < elem.length; i++) { if (elem[i].type != “button”) {
//its a button?
if (elem[i].name == “”) {
// i had no name
var c_name = elem[i].id; } else {
// i had a name
var c_name = elem[i].name; }
// If im a radio or check, the value checked is important to me
if (elem[i].type == “checkbox” || elem[i].type == “radio”) {
// apparently im a c-box or a radio”)
var checked = elem[i].checked; formName.elem[i].name.checked =getCookie(c_name) } else { var value = elem[i].value; formName.elem[i].name.Value=getCookie(c_name); } } else {
//I was i button, so dont bother to fill me
}
[/code]

the code lines consisting of “formName.elem[i].<name/id>.<value/checked> =getCookie(c_name)” crash the script.
It generates this error: “formName.elem has no properties”

I i am asumming it sees the line as “formName.elem .[i]<name/id> .<value/checked>”

How to get it to work?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 05.2007 — I assume getCookie returns [I]true[/I] or [I]false[/I]? elem[i] is a checkbox?

var bool = elem[i].checked; formName.elem[i].checked =getCookie(c_name)
Don't use variable names that can confuse the parser: http://javascript.about.com/library/blreserved.htm
Copy linkTweet thisAlerts:
@ZelthorauthorDec 05.2007 — First, thanks for looking in to the matter.

The getCookie function returns the value of the cookie containing the name of the argument supplied.

The code once again, i have refined the formatting a bit and updated the code(it was a slight older version)
[code=php]
function returnFormCookies(formName){
var elem = document.getElementById(formName).elements;
alert(elem.length);

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


if (elem[i].type != "button") { //its a button?
if (elem[i].name == "") {

var c_name = elem[i].id;
}
else {

var c_name = elem[i].name;

}
// radio or c-box?, then i would like to fill in the checked property
if (elem[i].type == "checkbox" || elem[i].type == "radio") {


Document.formName.elem[i].name.checked=getCookie(c_name);

}
else {

var value = elem[i].value;

Document.formName.elem[i].name.Value=getCookie(c_name)
}

}
else {
// it was a button, so dont bother filling
}
}[/code]


But the main problem is that it will do "document.BLARGH.checkbox1.checked=getCookie(c_name)"

But as soon as i make values "BLARGH" and "checkbox1" dynamic as seen in the code above it misinterprets it.

Again, thanks for helping
Copy linkTweet thisAlerts:
@FangDec 05.2007 — Document.formName.elem[i].name.checked=getCookie(c_name);[/QUOTE]

Incorrect referencing.

Not [COLOR="Red"]D[/COLOR]ocument but document

elem[i] is already a reference to the element, no need for document.formName

Either: elem[i].name=getCookie(c_name); // getCookie returns a valid name

or: elem[i].checked=getCookie(c_name); // getCookie returns a boolean
Copy linkTweet thisAlerts:
@ZelthorauthorDec 05.2007 — Thank You,

Implemented it, and it works like a charm.

Live long and prosper
Copy linkTweet thisAlerts:
@ZelthorauthorDec 05.2007 — Thank You,

Implemented it, and it works like a charm.

Live long and prosper[/QUOTE]

p.ps i found out that i was using checked="true/false" instead of checked= "checked/<nothing>"
×

Success!

Help @Zelthor 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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