jona
jona

Reputation: 722

Jquery IF statement and .is

i wanted to do something after a JQUERY have successfully run,

var Public =  $("a[data-tooltip-content*='Public']").closest(classes).css({"background-color": "yellow"});
      if($(this).is(Public)){
          CountPub = CountPub + 1;
          console.log(CountPub);
      }

This code should works as the following:

1) invoke a element with the colour yellow if the selector is correct
2) increase the counter by 1 everytime it has successfully invoked the css scripts

Upvotes: 0

Views: 55

Answers (2)

charlietfl
charlietfl

Reputation: 171698

This might be all you need, just counting the number of elements that match the selector by using length of resultant collection

var Public =  $("a[data-tooltip-content*='Public']").closest(classes).css({"background-color": "yellow"});

var CountPub = Public.length

Upvotes: 2

Bram Vanroy
Bram Vanroy

Reputation: 28584

It seems that you want to do this:

var CountPub = 0;
$("a[data-tooltip-content*='Public']").closest(classes).each(function(i, el) {
  $(el).css('background-color', 'yellow');
  CountPub++;
});

Upvotes: 1

Related Questions