2

Is there a way to select element by its data attribute using jQuery? I have a div

<div class="remove">delete</div>

And I've added data attributes to it like so:

$('.remove').attr('data-id', data._id);
$('.remove').attr('data-rev', data._rev);

What I want is to select this element based on its 'data-id' and 'data-rev' using jQuery.

Thanks!

5 Answers 5

2

Try like

$('.remove[data-id = "' + data._id + '"]');
$('.remove[data-rev = "' + data._rev + '"]');

If you want to check both the data then you can use

$('.remove[data-id = "' + data._id + '"][data-rev = "' + data._rev + '"]');
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

$('.remove[data-id = "' + data_id + '"][data-rev = "' + data_rev + '"]');

Comments

1
$('.remove[data-id="'+data._id+'"]');
$('.remove[data-rev="'+data._rev+'"]');

reference attribute-equals-selector

Comments

1

Try attribute-equals-selector

$('.remove[data-id="'+data._id+'"]')
$('.remove[data-rev="'+data._rev+'"]');

Comments

1

You can do this using .filter():

var $dataId = $('.remove').filter(function(){
    return $(this).data('id') === 'something';
});

var $dataRev = $('.remove').filter(function(){
    return $(this).data('rev') === 'something';
});

and then you can use the variables like:

$dataId.show();
$dataRev.hide();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.