?
Solved

How do I utilise GetBySelector on checkboxes?

Posted on 2008-11-05
1
Medium Priority
?
318 Views
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??
0
Comment
Question by:kgpretty
1 Comment
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 210 total points
ID: 22894176
Well you first need to include this script
http://simonwillison.net/2003/Mar/25/getElementsBySelector/

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' && item.name.indexOf('catalogItem')==0 && item.checked)?0:item.value
  }
  theForm.total.value=total.toFixed(2)
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses

839 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