www.webdeveloper.com
Results 1 to 5 of 5

Thread: Get the number of elements inside another element

  1. #1
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24

    Get the number of elements inside another element

    I'm working on an exercise I need to have finished pretty soon. The concept is simple: online bookstore with cart etc. It's written on php, using JS and mysql.

    Each time I view a book's details, I can add it to my cart, where I can also alter it's quantity.
    The code for my cart is this:

    PHP Code:
    <div style="margin-left:20px;">
        <p><h3>Your list of books to buy:</h3></p>
    <form id="place_order" action="Customer_Menu/plorder.php" method="post">
        <?php
        $cost 
    0;
        if(isset(
    $_SESSION['cart'])){
            foreach(
    $_SESSION['cart'] as $cartLine){ ?>
                <span><b>Title:</b><input type="text" id="title" name="title" value="<?php echo $cartLine['title']; ?>" size="50" disabled="disabled" style="background-color:#666633;border:none;color:white;"/><br/>
            <b>Price:</b><input type="text" id="price" name="price" value="<?php echo $cartLine['price']; ?>" size="1" disabled="disabled" style="background-color:#666633;border:none;color:white;"/>$<br/>
                    <b>Quantity:</b><select id="quant" onchange="javascript:ManageItem(this);">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select></span><hr/>
                <?php $cost += $cartLine['price']*$cartLine['quantity']; 
            }
        }
        
    ?> 
           Total Cost: <input type="text" id="total" name="total" size="1" value="" style="background-color:#666633;border:none;color:white;" disabled="disabled"/>$<br/>
        <input type="submit" id="order_btn" name="order_btn" value="Procceed to order!"/>
    </form>
    </div>
    As you can see, each time I choose a book to add it to my cart, a new span will be created, to hold the info of this book. What I need to do with the ManageItem js function, is to update the total cost at the bottom of the page, but I can't seem to be getting anywhere, so any help will be mostly appreciated.

    Here is what I've done so far:

    Code:
    function ManageItem(x){
    	var my_spans = document.getElementsByTagName("span");
    	var my_prices = new Array();
    	var my_quantity = new Array();
    	var total = 0;
    
    	for(var i=0;i<my_spans.length;i++){
    		//working with each span seperate
    		var this_span = my_spans[i].childNodes;
    
    		my_quantity[i] = x.selectedIndex.value;
    		my_prices[i] = parseInt(this_span.document.getElementById("price").value)*parseInt(my_quantity[i]);
    	}
    	for(var j=0;j<my_prices.length;j++){
    		total +=my_prices[j]; 
    	}
    	document.getElementById("total").value = total;
    	
    }
    Last edited by salvi; 11-27-2011 at 05:59 AM.

  2. #2
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24
    Changed my JS code to this:

    Code:
    function ManageItem(){
    	var my_spans = document.getElementsByTagName("span");
    	var this_span = new Array();
    	var my_prices = new Array();
    	var my_quantity = new Array();
    	var total = 0;
    
    	for(var i=0;i<my_spans.length;i++){
    		this_span[i] = my_spans[i].getElementsByTagName("*");
    		for(var j=0;j<this_span[i].length;j++){
    			my_quantity[i] = this_span[i].item(2).selectedIndex.value;
    			my_prices[i] = parseInt(this_span[i].item(1).value)*parseInt(my_quantity[i]);
    		}
    	}
    	for(var j=0;j<my_prices.length;j++){
    		total +=parseInt(my_prices[j]); 
    	}
    	document.getElementById("total").value = total;
    }
    But if i try to print my my_quantity[i] to check if the value is assigned correctly, I'm getting "undefined" :/

  3. #3
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    What you might want to do is use a <textarea> to store your cart information.

  4. #4
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24
    Wouldn't that be the same as using span?
    Each time I add a product, the page looks like that http://img341.imageshack.us/img341/6369/siteej.jpg

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    For the beginning I see a mistake: you create, in a PHP loop, elements which have the same id (id="title", id="price"...). Unlike the name attribute, the id must be an unique identifier on the same document/session.
    Last edited by Kor; 11-28-2011 at 03:33 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles