One technique would be to store the current state in the DOM, using data- attributes. That would mean putting the prices directly on the select elements, and querying them when one is chosen:
var button = document.getElementById("button");
var select = document.getElementById("select");
var basket = document.getElementById("basket");
var totalCost = document.getElementById("total-cost");
function addToBasket() {
var li = document.createElement("li");
li.innerHTML = select.options[select.selectedIndex].text;
var price = Number(select.options[select.selectedIndex].dataset.price);
li.dataset.price = price;
basket.appendChild(li);
totalCost.innerHTML = ((Number(totalCost.innerHTML) || 0) + price).toFixed(2);
}
document.getElementById("remove-button").onclick = function() {
var list = document.getElementById("basket");
var price = Number(list.childNodes[0].dataset.price);
list.removeChild(list.childNodes[0]);
totalCost.innerHTML = ((Number(totalCost.innerHTML) || 0) - price).toFixed(2);
}
button.addEventListener("click", addToBasket);
div {float: left; margin-left: 3em;}
<div id="select-items">
<form id="myForm">
<p>Item: <select id="select">
<option value="1" data-price="1.23" id="a">A</option>
<option value="2" data-price="2.35" id="b">B</option>
<option value="3" data-price="7.11" id="c">C</option>
<option value="4" data-price="9.87" id="d">D</option>
</select>
<button id="button" type="button">Add</button></p>
<button id="remove-button" type="button">Remove</button>
</form>
</div>
<div id="basket-total">
<p>Basket</p>
<div id="basket"></div>
</div>
<div id="total-of-basket">
<p>Total Cost</p>
<p id="total-cost"></p>
</div>
This shares the problem with the original that "remove" on an empty basket throws errors. That should be easy to fix, but is outside the scope of the problem.
I don't actually think that's your best bet. I think it would be better to keep the data in JS variables. But you'd need a way to get them there from whatever generated your dropdown. If this is manual, that's not an issue, but if this is generated server-side, you might need a dynamic generation of prices:
var button = document.getElementById("button");
var select = document.getElementById("select");
var basket = document.getElementById("basket");
var totalCost = document.getElementById("total-cost");
var prices = {a: 1.23, b: 2.35, c: 7.11, d: 9.87} // dynamically generated?
var total = 0;
function addToBasket() {
var li = document.createElement("li");
total += prices[select.options[select.selectedIndex].id];
totalCost.innerHTML = total.toFixed(2);
li.innerHTML = select.options[select.selectedIndex].text;
li.itemId = select.options[select.selectedIndex].id;
basket.appendChild(li);
}
document.getElementById("remove-button").onclick = function() {
var list = document.getElementById("basket");
total -= prices[list.childNodes[0].itemId];
totalCost.innerHTML = total.toFixed(2);
list.removeChild(list.childNodes[0]);
}
button.addEventListener("click", addToBasket);
div {float: left; margin-left: 3em;}
<div id="select-items">
<form id="myForm">
<p>Item: <select id="select">
<option value="1" data-price="1.23" id="a">A</option>
<option value="2" data-price="2.35" id="b">B</option>
<option value="3" data-price="7.11" id="c">C</option>
<option value="4" data-price="9.87" id="d">D</option>
</select>
<button id="button" type="button">Add</button></p>
<button id="remove-button" type="button">Remove</button>
</form>
</div>
<div id="basket-total">
<p>Basket</p>
<div id="basket"></div>
</div>
<div id="total-of-basket">
<p>Total Cost</p>
<p id="total-cost"></p>
</div>
This solution still stores some state in the DOM -- the id property of the select items and the itemId of the newly created basket items. But that's it. The calculations use those ids to find the correct price in the prices object, and they use the javascript total variable to store the current price.
Again, I don't try to solve the remove problem here.
Honestly, this is still not code I really like. I'm not a fan of the global variables, and would probably find a way to embed them in some other scope. And there is other cleanup I would do as well, but this might get you on your way.
{ "id1" : { name: "aaa", description: "bbb", price: "1.99" }, "id2" : {...}, "id3": {...}}but it can get more complicated if it has not stuff like colors, sizes, etc.