HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Showing an item in a shopping cart

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
itemcartshowingshopping

Problem

I'm updating and old project from jQuery 1.4 to jQuery 1.7.

Can this be simplified, thus reducing the amount of code, and perhaps in such reduction, have it improved?

Having the following code:

var cartItemDel   = ''+token+'',
    cartItemName  = ''+ name +'',
    cartItemPrice = ''+ price +'€';

$(".cart-table tbody")
    .append(''+cartItemDel+cartItemName+cartItemPrice+'');


I've updated it to:

/*
 * ADD NEW LINE - First TD
 */
var $newLineIcon = $("", {
        src    : "components/themes/default/img/icons/cross-small.png",
        width  : "16",
        height : "16",
        alt    : "X"
    });

var $newLineLID = $("", {
        type  : "hidden",
        class : "lid",
        name  : "lineID"+ lid,
        value : lid
    });

var $newLineToken = $("", {
        class : "hidden token",
        text  : token
    });

var $newLine_firstTD = $("", { class : "del"})
    .append($newLineIcon)
    .append($newLineLID)
    .append($newLineToken);

/*
 * ADD NEW LINE - Second TD
 */
var $newLinePID = $("", {
        type  : "hidden",
        class : "CartItemId",
        name  : "id_"+ id,
        value : id
    });

var $newLinePNAME = $("", {
        class : "CartItemName",
        text  : name
    });

var $newLine_secondTD = $("")
    .append($newLinePID)
    .append($newLinePNAME);

/*
 * ADD NEW LINE - Third TD
 */
var $newLinePREF = $("", {
        type  : "hidden",
        class : "CartItemRef",
        name  : "ref_"+ ref,
        value : ref
    });

var $newLinePPRICE = $("", {
        class : "CartItemPrice",
        html  : "€"
    })
    .prepend($("", {class : "lineSum", text : price}));

var $newLine_thirdTD = $("")
    .append($newLinePREF)
    .append($newLinePPRICE);

$("")
    .append($newLine_firstTD)
    .append($newLine_secondTD)
    .append($newLine_thirdTD)
    .appendTo($(".cart-table tbody"));

Solution

Your code looks OK to me, very clear and organised.

It's more important to minify your code to decrease load time. There's a whole bunch of minification tools on the Internet.

Context

StackExchange Code Review Q#13394, answer score: 4

Revisions (0)

No revisions yet.