I am trying to pass table id, div id and nested class names into a generic function that's used across the programme code.
The tricky part is, this function calls another function which is wrapped within an anchor tag's onclick event. Before adding the two additional parameters, the function removeItem
worked and now no longer fires. These two parameters are required in order to make the function reusable.
The jsfiddle of the expected programme.
Both ids (the parameters) are passed as strings quoted,
var tableid = "'#tble1200'"
var otherid = "'div.class1 .total'"
function AddProduct(elment, event, tableid, otherid)
{
//do something
//the hyperlink is remove
remove: '<a href="#" class="remove"
onclick="removeProduct(this, event,tableid,otherid)">X</a>'
}
function removeProduct(element, event, tblid, othid)
{
//do something
$(tblid).datagrid('loadData', data);
$(othid).html('Total: $'+subTotal);
}
I am trying out the code from this post's jsfiddle here. : The original code.
function addProduct(name,price){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price,
remove: '<a href="#" class="remove" onclick="removeProduct(this, event)">X</a>'
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}
For multiple carts to work independently you have to store cart data in and
array
withindex
. And pass theindex
toaddProduct
andremoveProduct
functions
. And for cart content useclass
instead ofid
Updated code is as below:
HTML
CSS
JS
JS Fiddle
functoin AddProduct(elment, event, tableid, otherid)
functoin is spelled incorrectly, it should be function.
Here is a solution that will work:
Using the
id
you can fetch the item later and bind a scope listener.See: http://jsfiddle.net/Vwu37/135/
Right now I'm passing
table_id
andcart_id
, you could consider passing the actual table and cart, ie.$("#cartcontent")
and$(".cart")
Keep in mind that
".cart"
is not unique, so if you want to have two lists you will need to make it unique.