Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need understanding of netscape issues with getElementByID...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i had originally built my page and tested on IE...getElementByID was working great, even though i had labeled a majority of my tags as NAME= instead of ID=.

so when it came time to run in Netscape, a majority of my scripts failed. i figured out the problem, and re-labeled all the elements to include BOTH the NAME= and the ID= attributes. (i need the NAME= for cgi purposes)

then i got everything to work great. now i came to a radio button page within my site that isn't working. the original script calls for accessing the radio buttons like so:

getElementById(radioBtn[1]).checked=true;

although this works in IE, Netcape isn't buying it. it says the radioBtn[1] isn't declared, or doesn't exist.

any ideas? i was reading up on this and came across the term getElementByTagName...although the post didn't directly address my problem, it seemed related. is this better to use in terms of wider acceptance (browser-wise)...

another question is: my code below adds (among other things) the new price of a product based on the new value entered into a text field. it then changes the total of the product, plus the updated grand total of all the products. the script works great in IE, and in Netscape it works but only on the individual product total. when it comes to the grand total, it reads $NaN.

maybe you can see a syntax error i missed?!?

Code:
function valChange(x) {

cartItemQty=eval("document.getElementById('prodQTY"+x+"').value");
cartItemQtySub=eval("document.getElementById('cartItemQtySub"+x+"').value");
cartItemQtyStatic=eval("document.getElementById('cartItemQtyStatic"+x+"').value");

itemQty=eval("document.getElementById('itemQty"+x+"').value");
qtyChange=eval("document.getElementById('cartItem"+x+"').value");
pricePer=eval("document.getElementById('pricePer"+x+"').value");

cartTotal2=eval("document.getElementById('cartTotal').value");
itemPrice=eval("document.getElementById('cartItemPrice"+x+"').value");
newItemTotal=(pricePer*(cartItemQty));//here is product total
newItemPrice=(cartItemQtySub*pricePer);

newItemTotalNum=Number(newItemTotal);

if (cartItemQty!=cartItemQtySub){
newCartTotal=Number(cartTotal2)-Number(newItemPrice)+Number(newItemTotal);//here is grand total
document.getElementById('cartTotal').innerHTML=newCartTotal.toFixed(2);
document.getElementById('cartTotal').value=newCartTotal;
}

eval("document.getElementById('cartItemPrice"+x+"').innerHTML=newItemTotal.toFixed(2)");
eval("document.getElementById('cartItemQtySub"+x+"').value=cartItemQty");

}

since it works in IE, it must be a syntax issue(?)...

thanks!

- g
 
Why are you using eval? document.getElementById takes a string as an argument.
Code:
function valChange(x) {

cartItemQty=document.getElementById('prodQTY'+x).value;
cartItemQtySub=document.getElementById('cartItemQtySub'+x).value;
cartItemQtyStatic=document.getElementById('cartItemQtyStatic'+x).value;

itemQty=document.getElementById('itemQty'+x).value;
qtyChange=document.getElementById('cartItem'+x).value;
pricePer=document.getElementById('pricePer'+x).value;

cartTotal2=document.getElementById('cartTotal').value;
itemPrice=document.getElementById('cartItemPrice'+x).value;
newItemTotal=(pricePer*(cartItemQty));
//here is product total

newItemPrice=(cartItemQtySub*pricePer);

newItemTotalNum=parseFloat(newItemTotal);

if (cartItemQty!=cartItemQtySub){
newCartTotal=parsefloat(cartTotal2)-parsefloat(newItemPrice)+parsefloat(newItemTotal);
//here is grand total
document.getElementById('cartTotal').value=newCartTotal;
}

document.getElementById('cartItemPrice'+x).innerHTML=newItemTotal.toFixed(2);
document.getElementById('cartItemQtySub'+x).value=cartItemQty;

}
See if this works for you.

--Chessbot
 

I'd change your code to read as below - even if it doesn't fix the issue.

Using eval is slow at the best of times, but using it in conjunction with document.getElementById in the way that you are is really unnecessary.

It is possible that other browsers are baulking over the Number typecasts, so to be on the safe side, I've converted them to be parseInt calls.

Code:
function valChange(x) {

	cartItemQty = document.getElementById('prodQTY' + x + ').value;
	cartItemQtySub = document.getElementById('cartItemQtySub' + x + ').value;
	cartItemQtyStatic = document.getElementById('cartItemQtyStatic ' + x + ').value;

	itemQty = document.getElementById('itemQty' + x + ').value;
	qtyChange = document.getElementById('cartItem' + x + ').value;
	pricePer = document.getElementById('pricePer"+x+"').value");

	cartTotal2 = document.getElementById('cartTotal').value");
	itemPrice = document.getElementById('cartItemPrice' + x + ').value;

	newItemTotal = (pricePer * (cartItemQty));	// here is product total
	newItemPrice = (cartItemQtySub * pricePer);

	newItemTotalNum = parseInt(newItemTotal, 10);

	if (cartItemQty != cartItemQtySub) {
		newCartTotal = parseInt(cartTotal2, 10) - parseInt(newItemPrice, 10) + parseInt(newItemTotal, 10);	// here is grand total
		document.getElementById('cartTotal').innerHTML = newCartTotal.toFixed(2);
		document.getElementById('cartTotal').value = newCartTotal;
	}

	document.getElementById('cartItemPrice' + x + ').innerHTML = newItemTotal.toFixed(2);
	document.getElementById('cartItemQtySub' + x + ').value = cartItemQty;
}

Hope this helps,
Dan
 

Of course, chessbot's code is better, as I totally forgot that you would be wanting to keep decimals, and thus parseFloat is far better than parseInt ;o)

Dan
 

That, and my lousy regexp search and replaces have left most trailing quotes in.

Aah well - it's one of those days!

 
But I went over it manually (no regex)...
I have too much time.

--Chessbot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top