17
<div class="row-container" data-i="'+data[i].product_id+'">
<div class="row-container" data-i="'+data[i].product_id+'">
<div class="row-container" data-i="'+data[i].product_id+'">
<div class="row-container" data-i="'+data[i].product_id+'">

I want to delete the row-container that contains a particular product_id. I know data('i') retrieves the value, but I dont know where to go from there.

Adam
  • 8,849
  • 16
  • 67
  • 131
  • Related post: http://stackoverflow.com/questions/1009485/jquery-filter-element-based-on-data-key-value – Cᴏʀʏ May 21 '12 at 19:20

3 Answers3

41
$('.row-container').filter(function(){
    return $(this).data('i') === "product_id"
}).remove();

.data allows you to set and get other variables beside of strings (functions and objects)

You can also use this:

$('.row-container').filter('[data-i="product_id"]').remove();

Or with one selector:

$('.row-container[data-i="product_id"]').remove();

(Where "product_id" is a placeholder for the real value...)

gdoron
  • 147,333
  • 58
  • 291
  • 367
5

You can use filter():

var $div = $(".row-container").filter(function() {
    return $(this).data("i") == value; // where value == product id to find
});
$div.remove();
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
5

There's an attribute selector in jQuery that you could use to find the element that you're looking for.

$('div.row-container[data-i="<value>"]');
albinohrn
  • 626
  • 3
  • 8