SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function not working

    Code JavaScript:
    function recalculateOrder() {
        var system_total = document.getElementById('system_total').value;
        var monitor = document.getElementById('monitors');
        var monitoroption = monitor.options[select.selectedIndex];
        var monitorprice = monitoroption.value.split(', ')[1];
        var keyboard = document.getElementById('keyboards_mice');
        var keyboardoption = keyboard.options[select.selectedIndex];
        var keyboardprice = keyboardoption.value.split(', ')[1];
        var speakers = document.getElementById('keyboards_mice');
        var speakersoption = speakers.options[select.selectedIndex];
        var speakersprice = speakersoption.value.split(', ')[1];
        var totalexcvat = monitorprice + keyboardprice + speakersprice;
        document.getElementById('total').value = totalexcvat.toFixed(2);
    }

    system_total is a hidden input.
    monitors, keyboards_mice, and speakers are a selects.
    total is an text input.

    This is being called by an onchange event handler on the three selects.
    For some reason it's just not working.
    Can anyone see why?
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  2. #2
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can we see the code that calls this function please? The three select boxes I guess.


  3. #3
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code HTML:
    onchange="recalculateOrder()"
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  4. #4
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so you're sure the function is being called when necessary then?

    Have you tried narrowing down which part of the function isn't working? Have you used an alert to see the value of totalexcvat before you put it in the text box? What about the values of monitorprice, keyboardprice and speakerprice before you add them together?

  5. #5
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    update

    Code JavaScript:
    function recalculateOrder() {
        var systemTotal = document.getElementById('system_total').value;
        var monitor = document.getElementById('monitors');
        var monitorOption = monitor.options[monitor.selectedIndex];
        var monitorPrice = monitorOption.value.split(', ')[1];
        var keyboard = document.getElementById('keyboards_mice');
        var keyboardOption = keyboard.options[keyboard.selectedIndex];
        var keyboardPrice = keyboardOption.value.split(', ')[1];
        var speakers = document.getElementById('keyboards_mice');
        var speakersOption = speakers.options[speakers.selectedIndex];
        var speakersPrice = speakersOption.value.split(', ')[1];
        var totalExcVat = systemTotal + monitorPrice + keyboardPrice + speakersPrice;
        document.getElementById('total').value = totalExcVat.toFixed(2);
    }

    Everything seems to be working, however totalExcVat is concatenating rather than adding together.
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  6. #6
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried this?:

    Code JavaScript:
        var totalExcVat = parseInt(systemTotal) + parseInt(monitorPrice) + parseInt(keyboardPrice) + parseInt(speakersPrice);

  7. #7
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the ticket, however I needed to use parseFloat rather than parseInt.

    Many Thanks
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.


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
  •