This code works perfectly fine with some changes. I was using chrome and later found out that
Google Chrome doesn't create cookies when the file is on the
local machine
and loaded in browser directly using file path.
Rather try with localhost.
It is definitely working when you put the code in server. Chrome became pain in a b*t here!
If you were for idea on creating shopping cart with Javascript follow this link. http://www.smashingmagazine.com/2014/02/create-client-side-shopping-cart/