How do I utilise GetBySelector on checkboxes?

Posted on 2008-11-05
Last Modified: 2009-12-16
I'm trying to do utilise a shopping cart using javascript and xml.

First, I am printing a catalog of items from my xml database onto the page. For each record, I am printing the name of the item, the price and a checkbox. The checkbox is ofcourse <input type='checkbox' id=''/> The id value is the corresponding id number of the item.

What I would like is that the user selects an item on the catalog, by clicking the checkbox next to the name & price and then clicking an Add to cart button.

Clicking the Add to Cart button should call the add() javascript function. What I would like is that for all the checkboxes selected from the catalog, each item is added to the shopping cart.

How do I accomplish this using getElementBySelector??
Question by:kgpretty
    1 Comment
    LVL 75

    Accepted Solution

    Well you first need to include this script

    since getElementBySelector is not a dom javascript call.

    I would use getElementsByTagName or forms access and a simple loop (assuming here that each relevan checkbox is named something starting with catalogItem...):
    function add(theForm) {
      var total = 0;
      for (var i=0;i<theForm.elements.length;i++) {
        var item = theForm.elements[i];
        total+=(item.type=='checkbox' &&'catalogItem')==0 && item.checked)?0:item.value

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now