Frederik Sohnis
Frederik Sohnis

Reputation: 1095

If data tag equals 1 hide link

I'm messing around a bit with chrome extensions and am trying to clean up webpages I use a lot. I'm trying to remove certain parts of a list and can see that they are positioned using html data tags.

<a class"Somelist" href"..." data-tid="1">1st</a>
<a class"Somelist" href"..." data-tid="2">2nd</a>
<a class"Somelist" href"..." data-tid="3">3rd</a>

How could I use jQuery to hide the link if the link equals 1?

I was trying to use something like

`if ( $('.Somelist').data("tid") == 1 ) { // hide }`

which obviously does not work. Does anyone know a way that would work? Thank you!

Upvotes: 2

Views: 89

Answers (2)

Christophe Marois
Christophe Marois

Reputation: 6719

You can select by using an attribute selector (MDN):

$('a[data-tid="1"]').hide();

Upvotes: 2

Quentin Roger
Quentin Roger

Reputation: 6538

You can try to do something like this :

$(document).ready(function(e) {
  $('.Somelist').each(function(i,el) {
    if ($(el).data("tid") == 1) {
      alert($(el).data("tid"));
      $(el).hide()
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="Somelist" href "..." data-tid="1">1st</a>
<a class="Somelist" href "..." data-tid="2">2nd</a>
<a class="Somelist" href "..." data-tid="3">3rd</a>

The explanation now :

When you use $('Somelist') it return an array with all elements corresponding to this selector

enter image description here

Upvotes: 2

Related Questions