Forum Moderators: open
My snippet of JS which is half working (can not restore innerHTML of remtd (previous <td>))
function showCart(id) {
if(typeof( oldid ) != 'undefined') {
remtd = document.getElementById(oldid);
remtd.removeChild(remtd.childNodes[0]);
var nestdiv = document.createElement("div");
nestdiv.innerHTML = "<a rel='noindex, nofollow' class='basket-add'><span><span>To basket</span></span></a>";
remtd.appendChild(nestdiv);
} else {
oldid = id;
}
var partId;
partId = id.match(/\d+/, id);
var td;
td = document.getElementById(id);
td.removeChild(td.childNodes[0]);
var newdiv = document.createElement("div");
newdiv.innerHTML = "<form action='addtocart.php' method='GET'>Кол-во: <input class='quant' type='text' name='quantity'><br /><input type='hidden' name='url' value='"+location.href+"'><input type='hidden' name='id' value='"+partId+"'><input class='proceed' type='submit' name='go' value='Ok'></form>"
td.appendChild(newdiv);
oldid = id;
}
Maybe someone knows how make it working? Thanks beforehands.
I've read if declare variable without keyword var it is assigned as global
Rather than relying on this, explicitly declaring global's makes your intent a lot clearer
eg
var oldid = null; // declare global
function showCart(id) {
if( oldid != null ) {
It is not clear how persistent you need oldid to be, if you need it to persist between page loads, consider keeping it as form data or cookie.