Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts,

I have print some html content using jquery.. Here is the Below code....
JavaScript
 $("#cart-grid").append("<tr class = 'cart_table_item'> " +
                        "<td class='product-remove'>  " +
                        "<a href='' class='remove' title='Remove this item'>
<i class='icon-remove'></i></a></td>  " +
                        "<td class='product-thumbnail'>  " +
                       "<a href='Item_desc.aspx?itmid=" + _itemID + "'>  " +
                       "<img width='90' height='90' src='wp-content/uploads/90x90/" + _picName + "' class='attachment-shop_thumbnail wp-post-image' /> " +
                       " </a></td> " +
                       "</tr>")
    });

And here is the below piece of javascript code to remove a table row from the above printed content....
JavaScript
$(".remove").on("click", function (e) {
        alert("asd");
        var href = $(this).attr('href');
        $(this).closest("tr").remove(); // remove row
        return false; 
});

But when i clicked the remove link then the above event will not be fired.... when i hard coded then the event will be fired.....

PLease Help me..

Thanks and Regards,
Dileep
Posted
Updated 26-May-13 22:52pm
v3

I don't know whether it is pasting problem while posting the code or not, but there are some problems.
JavaScript
"<a href="" class="remove" title="Remove this item">"></a>  " +

Here the underlined part is extra. And there is no text for the link so I have added that. Now the code is working fine. Means alert is coming and the tr is getting removed.

Below is the total code what I have tried. It is working fine. You can check.
XML
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                    $("#cart-grid").append("<tr class = 'cart_table_item'> " +
                        "<td class='product-remove'>  " +
                        "<a href='' class='remove' title='Remove this item'>Remove</a></td>  " +
                        "<td class='product-thumbnail'>  " +
                        "<a href='Item_desc.aspx?itmid=" + 1 + "'>  " +
                        "<img width='90' height='90' src='wp-content/uploads/90x90/" + "test" + "' class='attachment-shop_thumbnail wp-post-image' /> " +
                        " </a></td> " +
                        "</tr>");

                    $(".remove").on("click", function (e) {
                            alert("asd");
                            //var href = $(this).attr('href');
                            $(this).closest("tr").remove(); // remove row
                            return false;
                    });
            });
        </script>
    </head>
    <body>
        <div id="cart-grid"></div>
    </body>
</html>
 
Share this answer
 
v2
Comments
dilzz 26-May-13 0:05am    
Sir, Its my mistake when i copy the code... i have changed the code.... but it is not working sir....... please help me..

Thanks...
Have you tried with my code ? It is working perfectly.

You can check the demo - http://jsfiddle.net/taditdash/KAbHe/.
I have included the Code Project Logo as the image.

You can check the console tab of FireBug in FireFox, if there are any errors or not.

I guess you have problem with the variables _itemID and _picName.
Use .click event as follows

$(".remove").click(function() {
alert("asd");
var href = $(this).attr('href');
$(this).closest("tr").remove(); // remove row
return false;

});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900