SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Problem in IE

    I'm writing a script for a price calculator. It works fine in most browsers, but when it comes to IE it doesn't total properly. You can find it at http://www.aedisweb.com/pricecalculator.php . I've done extensive debugging and I can't figure out exactly what's wrong. If there's anyone out there who can shed some light on this problem or help figure out how to fix it I'd really appreciate it.

    Quick explanation of how it works: It finds all inputs on the page with the class out and throws them into an array. Then it takes all the items in the array and does a parseFloat() to get the value in each of the outboxes and adds them together. Somewhere in the process IE is messing up. For details, just look at the script.

    Note: prices are mostly dummy figures. I've just thrown most of them in there as numbers to work with.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bjk2007 View Post
    I'm writing a script for a price calculator. It works fine in most browsers, but when it comes to IE it doesn't total properly.
    Code:
    function updateTotal()
      {
      a = 0;
      amount = new Array();
      inputs = document.getElementById('pricecalc').getElementsByTagName('input');
      for (i in inputs)
        {
    i will be assigned not just integers but properties of the inputs collection. It just happens that I.E. enumerates the properties first.
    Try alerting i and see what happens.
    You must use i as a counter.

    <!-- --> comments are no longer required in JS.

    Alway declare variables with var, or you'll get into trouble eventually.

    You have several instances of calling getElementById instead of document.getElementById.

    Don't assume that parseFloat will return a number.

    To increment a variable, you need mention it only once: v+=n;
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I'm new at javascript coming off of PHP, so you can see a little of it blending into the script. I'm really missing its predictability and consistency.

    I got a pretty bad understanding of for...in loops from http://www.w3schools.com/js/js_loop_for_in.asp. When I alerted i, I got quite different results from IE and Firefox. Would using an incrementing var in some kind of loop be better for my purpose that a for..in loop?
    I put the html comments in for the js just to avoid all the annoying errors & warnings I get when I validate. I figured no browser in common use needs them but I left them in anyway.
    I fixed the getElementById's not starting from the document object, not sure why I did that. Might have missed it somewhere and multiplied it with copy and paste.
    I knew that parseFloat would end up causing problems, I just haven't gotten that far in the script to worry about it. I don't know if the script is doing it right now, but I was ending up with NaN not a number whenever I totaled, even though writing the typeof parseFloat to the string returned only several occurences of the number type. I'll worry about that a bit more than I get there.
    Quote Originally Posted by Logic Ali
    To increment a variable, you need mention it only once: v+=n;
    Quick question- Is there any reason I should use the += assigner instead of the ++ operator? I just went with the ++ operator out of habit.

    I uploaded the modified script. I'll play with the for...in loop when I get time. I've got a PHP newsletter script I'm working on right now as well that is taking up most of my time, but I'll work on this when I get the chance since I've got a bit of help now. Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •