By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

going bonkers with ID and NN and document.form.element.value

P: n/a
This is driving me nuts....I can't find a solution that works for the
following:

---------------

var tbl = document.getElementById("question1b");
var choiceOne = ["Q1a_1", "Q1a_2", "Q1a_3", "Q1a_5", "Q1a_7", "Q1a_9",
"Q1a_10",];
var choiceTwo = ["Q1b_1", "Q1b_2", "Q1b_3", "Q1b_5", "Q1b_7", "Q1b_9",
"Q1b_10",];
var choiceName = ["name1", "name2", "name3", "name4", "name5", "name6",
"name7",];

var codeString = "";
for ( i=0; i<=choiceOne.length-1; i++ ){
var choice = choiceOne[i];
if (document.getElementById(choice).value == 1){
codeString = codeString + "<label class='choice-text'><input
type='checkbox' name='"+ choiceTwo[i] +"' id='"+ choiceTwo[i] +"'
value='1' />"+ choiceName[i] +"</label><br />";
}
}

----------------

The above code works fine with NN6. It bombs with IE, "Object required".

The sticking point is this line:

if (document.getElementById(choice).value == 1){

If I change the line to:

if (document.getElementById('Q1a_1').value == 1){

it no longer bombs in IE, but it does not work in the fashion that I need
it to.

Am I missing the boat here? Is there some part of the naming structure I
am ignorant of or a different way to refer to the element that IE will
accept? I refuse to believe that I can't use a variable in place of the
element name since that is such a basic functionality.....
Aargh.
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"stickdoctorq" <wo******************@fuckspammersdiediedie.com> wrote in
message news:Xn**********************************@24.69.25 5.211...
<snip>
var choiceOne = ["Q1a_1", "Q1a_2", "Q1a_3", "Q1a_5",
"Q1a_7", "Q1a_9","Q1a_10",];

^

Your Array literal has a trailing comma after the last defined entry.
That is supposed (by ECMA 262) to create an additional Array element,
leaving this array 8 elements long instead of 7 and with choiceOne[7]
being undefined. That is bound to produce undesirable results as you
loop through the 8 elements of the array.

<snip>

Richard.
Jul 20 '05 #2

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in
news:br*******************@news.demon.co.uk:
"stickdoctorq" <wo******************@fuckspammersdiediedie.com> wrote
in message news:Xn**********************************@24.69.25 5.211...
<snip>
var choiceOne = ["Q1a_1", "Q1a_2", "Q1a_3", "Q1a_5",
"Q1a_7", "Q1a_9","Q1a_10",];

^

Your Array literal has a trailing comma after the last defined entry.
That is supposed (by ECMA 262) to create an additional Array element,
leaving this array 8 elements long instead of 7 and with choiceOne[7]
being undefined. That is bound to produce undesirable results as you
loop through the 8 elements of the array.

<snip>

Richard.


I don't believe it. I KNEW it would turn out to be something simple
like this, but, for the life of me I could not track down the source of
the problem.

You have it exactly right. For whatever reason, Mozilla Firebird is
more forgiving than IE 6, and overlooked the additional array element,
whereas IE went looking for another array element while looping, and
choked when it didn't find it, or when it found a NULL value for the
additional array element.

At any rate, it works now...and I am saved from putting my fist through
my boss's monitor....

Jason
Jul 20 '05 #3

P: n/a
stickdoctorq <wo******************@fuckspammersdiediedie.com> writes:
var choiceOne = ["Q1a_1", "Q1a_2", "Q1a_3", "Q1a_5", "Q1a_7", "Q1a_9",
"Q1a_10",]; ^ this extra comma,
increases the length of the array in IE, but not in Mozilla/Netscape
6+ (or Opera 7).

While stupid, Mozilla and Opera's behavior is in compliance with
the ECMAScript standard. If they had just dropped that extra ","
from the rule
ArrayLiteral : [ ElementList , Elision_opt ]
it would have made much more sense. Alas, they didn't, and *that*
comma isn't counted towards the end of the array. IE counts it.

That means that your for loop ends up indexing a non-existing
array element, making "choice" have the value "undefined".
*Then* the next line fails.
If I change the line to:

if (document.getElementById('Q1a_1').value == 1){

it no longer bombs in IE, but it does not work in the fashion that I need
it to.
That's because the problem is at the end of the array, the first element
works fine.
Am I missing the boat here? Is there some part of the naming structure I
am ignorant of or a different way to refer to the element that IE will
accept? I refuse to believe that I can't use a variable in place of the
element name since that is such a basic functionality.....
Just a comma. :)
Aargh.


Indeed!
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
Your Array literal has a trailing comma after the last defined entry.
That is supposed (by ECMA 262) to create an additional Array element,


Actually not (to my surprise, when I read up on it to answer this question :).

The relevant rule is:
---
The production ArrayLiteral : [ ElementList , Elision_opt ] is
evaluated as follows:

1. Evaluate ElementList.
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Get]] method of Result(1) with argument "length".
4. Call the [[Put]] method of Result(1) with arguments "length" and
(Result(2)+Result(3)).
5. Return Result(1).
---

The length of [1,2,3,] is the same as the length of [1,2,3], because
the literal comma in this grammar production isn't counted.

The grammar rule would work just as well without the comma (except
that [1,2,3] would then be ambiguous), or with an additional +1.
I say it is an error, but it is an official error. :(

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:ek**********@hotpop.com...
Your Array literal has a trailing comma after the last defined
entry. That is supposed (by ECMA 262) to create an additional
Array element,


Actually not (to my surprise, when I read up on it to answer this
question :).

<snip>

Yes, I have just been looking at the spec and you are right, a final
comma should not effect the length or contents of an array. It looks
like Microsoft messed up their ECMA 262 implementation (again), but I
remember a final comma adding an extra (undefined) element on one of the
early (from around the end of 2001) releases of Netscape 6.

Richard.
Jul 20 '05 #6

P: n/a
> >> var choiceOne = ["Q1a_1", "Q1a_2", "Q1a_3", "Q1a_5",
"Q1a_7", "Q1a_9","Q1a_10",];
Your Array literal has a trailing comma after the last defined entry.
That is supposed (by ECMA 262) to create an additional Array element,
leaving this array 8 elements long instead of 7 and with choiceOne[7]
being undefined. That is bound to produce undesirable results as you
loop through the 8 elements of the array.
I don't believe it. I KNEW it would turn out to be something simple
like this, but, for the life of me I could not track down the source of
the problem.

You have it exactly right. For whatever reason, Mozilla Firebird is
more forgiving than IE 6, and overlooked the additional array element,
whereas IE went looking for another array element while looping, and
choked when it didn't find it, or when it found a NULL value for the
additional array element.

At any rate, it works now...and I am saved from putting my fist through
my boss's monitor....


JSLINT checks for that, as well as many other problems.

http://www.crockford.com/javascript/lint.html
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.