arrays in Firefox 5.0

P

Peter W Johnson

Hi Guys,

I have a bit of script that puts shop purchase into an array for processing.

The code follows:-

<script language="JavaScript" type="text/JavaScript">
<!--
function SubmitButton() {

var oFrm = document.online_shop;
// make array
var sale_item = new Array();
var description = new Array();
var quant = new Array();
var price = new Array();
var tax = new Array();
var total = new Array();
var table_id = "shop";
var i;
var numrows = document.getElementById(table_id).rows.length - 1; //
don't count the header row!
for (i = 1; i <= numrows; i++) {
sale_item = (document.getElementById('sale_item_' + i).value);
.....................stops here!!!!
description = (document.getElementById('description_' + i).value);
quant = parseInt(document.getElementById('quant_' + i).value);
price = parseFloat(document.getElementById('price_' + i).value);
tax = parseFloat(document.getElementById('tax_' + i).value);
total = parseFloat(document.getElementById('total_' + i).value);
}
.......etc.......


This works perfectly in IE 8.0 but stops at the sale_item =
(document.getElementById('sale_item_' + i).value); line when using Firefox
5.0.

Any ideas as to how I can change this for Firefox users?

Cheers

Peter
 
S

Stanimir Stamenkov

Sat, 30 Jul 2011 16:11:05 +1000, /Peter W Johnson/:
This works perfectly in IE 8.0 but stops at the sale_item =
(document.getElementById('sale_item_' + i).value); line when using
Firefox 5.0.

Any ideas as to how I can change this for Firefox users?


Do you see any errors in the Error Console (Tools -> Error Console)?
May be it doesn't find an input element with id of ('sale_item_' +
i) so accessing .value further fails. If you could provide an URL
to a complete example, it would help.
 
E

Evertjan.

Peter W Johnson wrote on 30 jul 2011 in comp.lang.javascript:
Hi Guys,

I have a bit of script that puts shop purchase into an array for
processing.

The code follows:-

<script language="JavaScript" type="text/JavaScript">

Why do you use last century's code? Use:


Why do you use last century code?

Leave this <!-- out
count the header row!
for (i = 1; i <= numrows; i++) {
sale_item = (document.getElementById('sale_item_' + i).value);


Why the outer ()'s?
sale_item = (document.getElementById('sale_item_' + i).value);
....................stops here!!!!


What do you mean by "stops here"?
Does it give an error?
At which loop?

dit you put in descriptive breakpoins, like:

alert('1: '+ 'sale_item_' + i)
alert('2: '+ document.getElementById('sale_item_' + i).value)
description = (document.getElementById('description_' + i).value);
quant = parseInt(document.getElementById('quant_' + i).value);
price = parseFloat(document.getElementById('price_' + i).value);
tax = parseFloat(document.getElementById('tax_' + i).value);>

total = parseFloat(document.getElementById('total_' + i).value);
}
......etc.......


This works perfectly in IE 8.0 but stops at the sale_item =
(document.getElementById('sale_item_' + i).value); line when using
Firefox 5.0.


Again:
What do you mean by "stops here"?

If an error is given you could try to narrow it down with:

temp ='sale_item_' + i;
alert(temp);
temp = document.getElementById(temp);
alert(temp);
temp = temp.value;
alert(temp);
sale_item = temp;

Any ideas as to how I can change this for Firefox users?

Wrong idea, you should write correct code for conforming browsers and then
look if IE will give an acceptable result.
 
S

SAM

Le 30/07/11 08:11, Peter W Johnson a écrit :
Hi Guys,

I have a bit of script that puts shop purchase into an array for
processing.

The code follows:-
sale_item = (document.getElementById('sale_item_' + i).value);
....................stops here!!!!


that element has no ID !
(may be a NAME but not an ID)

IE thinks that
name === id
:-(
and all other browsers think it's different !

Surely your elements are in a form, no ?
So, prefer to use the old JS way(*), even if it is slower :

var f = document.forms['myFormName'].elements;
// or f = document.forms[0].elements;
// or f = document.getElementById('myFormId'); // it depends
while(blah) {
sale_item = f['sale_item_' + i].value;
description = f['description_' + i].value;
and so...
}


(*) tree of forms : document.forms[]
that works everywhere with "names"
 
S

SAM

Le 30/07/11 08:11, Peter W Johnson a écrit :
Hi Guys,

I have a bit of script that puts shop purchase into an array for
processing.

The code follows:-

<script language="JavaScript" type="text/JavaScript">
<!--

function SubmitButton() {

var oFrm = document.online_shop;
// make array
var sale_item = new Array();
var description = new Array();
var quant = new Array();
var price = new Array();
var tax = new Array();
var total = new Array();
var table_id = "shop";
var i;
var numrows = document.getElementById(table_id).rows.length - 1; //
don't count the header row!

i = numrows;

while(i--) {

if(document.getElementById && // remember NC.4 doesn't understand DOM
document.getElementById('sale_item_' + i))
sale_item = document.getElementById('sale_item_' + i).value;
else {
alert('no item with id = "sale_item_'+i+'"');
break;
}
if(document.getElementById &&
document.getElementById('description_' + i))
description = document.getElementById('description_'+i).value;
else {
alert('no item with id = "description_'+i+'"');
break;
}

etc.
quant = parseInt(document.getElementById('quant_' + i).value);
price = parseFloat(document.getElementById('price_' + i).value);
tax = parseFloat(document.getElementById('tax_' + i).value);
total = parseFloat(document.getElementById('total_' + i).value);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top