Solved

How do I add remove item buttons in my JavaScript shopping cart?

Posted on 2010-11-22
2
327 Views
Last Modified: 2012-05-10
I have wrote the following 2 JavaScript codes for a shopping cart. I need to know how to add 3 remove buttons so that the 3 products can be removed by the user from the shopping cart.
detail.html
cart.html
0
Comment
Question by:jbosarg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 34195981
1. change
<a href="javascript:place('1 - 10 count bag of Red Balloons')">Put In Shopping Cart</a>&nbsp;&nbsp;

to

<a href="#" onclick="return place('1 - 10 count bag of Red Balloons')">Put In Shopping Cart</a>&nbsp;&nbsp;

2. add

<a href="#" onclick="return remove('1 - 10 count bag of Red Balloons')">Remove from Shopping Cart</a>&nbsp;&nbsp;


3 change
function place(new_item){
  balloons_string = balloons_string + "xxx" + new_item;
  document.cookie = "balloons=" + escape(balloons_string);
}

to

function place(new_item){
  var balloons_string = getCookie('balloons');
  balloons_string += "xxx" + new_item;
  setCookie("balloons",balloons_string);
}
function remove(new_item){
  var balloons_string = getCookie('balloons');
  balloons_string = balloons_string.replace("xxx" + new_item,""); // remove once
  setCookie("balloons",balloons_string);
}

and use this code:

// cookie.js file
var cookieToday = new Date(); 
var expiryDate = new Date(cookieToday.getTime() + (365 * 86400000)); // a year

/* Cookie functions originally by Bill Dortsch */

function setCookie (name,value,expires,path,theDomain,secure) { 
   value = escape(value);
   var theCookie = name + "=" + value + 
   ((expires)    ? "; expires=" + expires.toGMTString() : "") + 
   ((path)       ? "; path="    + path   : "") + 
   ((theDomain)  ? "; domain="  + theDomain : "") + 
   ((secure)     ? "; secure"            : ""); 
   document.cookie = theCookie;
} 

function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      var offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         var end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   } 
} 
function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34199086
you will need to update the cart to use the same cookie script and do :
<script type="text/javascript" src="cookie.js"></script>
<script language="javascript">
  var whole_balloon_cookie = getCookie("balloons");
  if (whole_balloon_cookie == null || whole_balloon_cookie =="") alert("You haven't ordered anything.");
  function process_cookie(){
    if (whole_balloon_cookie != "") {
      balloon_array = whole_balloon_cookie.split("xxx");
      if (balloon_array.length < 2) {
        balloon_array = "";
      }
    }
    window.location = "cart.html";
  }
</script>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

717 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