SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Shopping List script

    Has anyone come across a script which would do the following:

    1. Select box with series of options.
    2. When user chooses an item from the select box it "writes" that item to a second list (without removing it from the first).
    3. Writes a running total of the value of the items (price would be in value of option item).

    I've come across several scripts that nearly do it - but my JS isn't good enough to get it to do everything I need.
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    That sounds like a shopping cart kind of thing, where you could use
    Code javascript:
    document.getElementById('shoppingList').onchange = addToCart;
    to add an item to the list with
    Code javascript:
    function addToCart() {
        var cartItem = this.options[this.selectedIndex].cloneNode(true);
        var cart = document.getElementById('shoppingCart');
        cart.appendChild(cartItem );
        updateTotal();
    }
    to add them to the other list and then perhaps
    Code javascript:
    function updateTotal() {
        var cart = document.getElementById('shoppingCart');
        var cartTotal = document.getElementById('shoppingTotal');
        // clear old total
        while (cartTotal.firstChild) {
            cartTotal.removeChild(cartTotal.firstChild);
        }
        // add up totals
        var total = 0;
        for (var i = 0; i < cart.options.length; i++) {
            total += Number(cart.options[i].value);
        }
        // create new total
        totalText = document.createTextNode('Total amount: ' + total);
        cartTotal.appendChild(totalText);
    }

    That's just from the top of my head, but it should give you a good start.
    Last edited by paul_wilkins; Feb 15, 2008 at 18:53.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for this - I'll work through it and see what I get!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project


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
  •