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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Works in FF and Safari, but not in IE 2

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi all,

This is driving me nuts. The following validation and recalc routine works fine in Firefox and Safari. But IE6 and IE7 - nothing happens. I can't even get IE to tell me an error message. I can only get a slight guess that it's because I am using DOM, but I thought IE has the DOM model in place.

Help! :)


<script type="text/javascript">
function recalc() {
var frm = document.forms[0];
for (i=0; i <frm.elements.length; i++){
if (frm.elements.type=="text" && frm.elements.id.match("Quant")!==null && (frm.elements.value=="" || isNaN(frm.elements.value))){
alert ("Must enter numeric quantities (ex. enter 1 instead of one)");
return false;
}
}
//recalc
var total=0;
var row_count;
var quant;
var price;
var clls;
var txt;
var txt2;
var tbl=document.getElementById("myTable");
row_count = tbl.rows.length;
for (i=1;i<row_count-2; i++){
clls = tbl.rows.cells;
txt = clls[0].getElementsByTagName('input')[0];
txt2 = clls[3].getElementsByTagName('input')[0];
if (txt.type=="text" && txt.id!=="txtPromoCode"){ // && txt.id.match("Quant")!==Null){
quant = Math.abs(Math.round(txt.value));
price = Math.abs(txt2.value);
txt.value=quant;
amt=(quant * price).toFixed(2);
clls[4].innerHTML=amt;
total = (total + (quant * price));
}
}
frm.elements["GrandTotal"].value=total.toFixed(2);
return true;
}
</script>

If you wish to see this in action go to Add a couple of lampes to the cart (just click the add to cart icon below an item). Then on the menu on the left, click View Cart. Try to recalc or enter a letter, etc.
 
1) why do you refuse to indent? refusing to indent makes your code SO much harder to read.

2) i've rarely, if ever, seen the "!==" used in javascript. replace that with a more common "!=" and see if that helps.


----
star.gif
 
In the first "if" for the recalc function, put an else in play - and alert something. That will let you know whether the "if" worked properly.

Debugging in IE is a pain - alert is a useful tool for this browser.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
OK I fixed this - call this one for the books...


Originally I had the two buttons on the page (Recalc - a custom button, and the submit button) - both calling the same recalc() routine. When the Recalc button was clicked in FF or Safari, it would recalc the amounts on the page. When the submit button was clicked it would recalc and then follow the action of the form - to the next checkout page. This was done by having the form tag have onSubmit="return recalc()"

I copied the recalc routine as is, and pasted it into the page again - named as recalc2. I changed the custom button to be onClick="return recalc2()".

That did it! IE would not run the same routine called from two buttons. Once I gave each button it's own routine to call, it now works in all browsers. This is inefficient because the page now has two identical js functions, just named differently. But what the heck - it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top