SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,692
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Problem with Javascript in Firefox

    I have a problem on http://www.panzerworld.net/PANZERINS.../orderform.php

    There is a small script in the header
    Code:
       function updatePrice() {
        quantityI = document.getElementById('quantityI').value;
        quantityII = document.orderForm.quantityI.value;
        quantityIII = document.getElementById('quantityIII').value;
        quantityIV = document.getElementById('quantityIV').value;
        quantityV = document.getElementById('quantityV').value;
        quantityVI = document.getElementById('quantityVI').value;
        document.getElementById('price').innerHTML = (quantityI * 35 + quantityII * 66 + quantityIII * 66 + quantityIV * 44 + quantityV * 55 + quantityVI * 39) + ',00';
       }
    which access the input boxes, and updates the total price. It works in IE, but I can't get it to work in Firefox. I tried to access the input fields using the form and field names (i.e. quantityI = document.orderForm.quantityI.value;)

    I don't have that much experience with Javascript, so I'm not sure where to start. (If it's because it's non-compliant code, I would love if someone could explain how to convert it into DOM)
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,422
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    id and name attributes

    I think some browsers use "name" and others use "id". You could try using both name="quantityI" and id="'quantityI" etc. in the form inputs.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    quantityI = document.getElementById('quantityI').value;
    There is no html element on your page that has an id attribute whose value is 'quantityI'.


    Code:
    quantityII = document.orderForm.quantityI.value;
    That will work when there is a form with a name attribute whose value is 'orderForm' and inside that form there is an html element whose name attribute has the value 'quantityI'. (Note: that seems to be duplicating what you tried in the first line.) The DOM way would be:
    Code:
    quantityII = document.forms['orderForm'].elements['quantityI'].value;
    Every page has a forms[] array containing all the forms on the page, and every form has an elements[] array containing all the user interaction elements in the form. The elements in both arrays can be accessed using the name attribute as the array index value, e.g. forms['orderForm']; or the array index value can be a numerical index corresponding to the position of the html element on the page, e.g. forms[0] where the first form on the page is at index position 0 in the forms[] array, and the second form on the page is at index 1 in the forms[] array, etc.


    Code:
    quantityIII = document.getElementById('quantityIII').value;
    quantityIV = document.getElementById('quantityIV').value;
    quantityV = document.getElementById('quantityV').value;
    quantityVI = document.getElementById('quantityVI').value;
    Same problem as the first line

  4. #4
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,692
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It works now, and I was even able to switch to the standards-compliant 'ID' attribute. What's even better, I think this solves several other bugs I've had in the past. Thanks guys!
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


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
  •