Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to remove div tag based on data attribute

Tags:

jquery

<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.

like image 402
Adam Avatar asked May 21 '12 19:05

Adam


People also ask

Can Div have data attribute?

An <div> element can have any number of data-* attributes, each with their own name. Using data-* attributes reduces the need for requests to the server.

How do I delete data attributes?

Make use of the removeAttribute() method to delete the given data attribute: el. removeAttribute('data-foo'); Apart from setting, getting, and erasing data values, all three methods are also used for manipulating other element attributes.

How to remove div data in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.


3 Answers

$('.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...)

like image 87
gdoron is supporting Monica Avatar answered Nov 16 '22 02:11

gdoron is supporting Monica


You can use filter():

var $div = $(".row-container").filter(function() {
    return $(this).data("i") == value; // where value == product id to find
});
$div.remove();
like image 29
Rory McCrossan Avatar answered Nov 16 '22 00:11

Rory McCrossan


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>"]');
like image 32
albinohrn Avatar answered Nov 16 '22 00:11

albinohrn