473,387 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 3132
"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
"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
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
"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
"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
> >> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
2
by: Dave | last post by:
I have a page which uses JavaScript to create form elements using document.createElement('input'), etc.. Both Firefox and IE have no problem accomplishing this and when the form is submitted all...
15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
5
by: terence.parker | last post by:
I have a PHP application which I wrote last year - and the JavaScript worked fine then. For some reason, now it doesn't - neither on IE nor Firefox. Has something changed? When I click on my...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
5
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.