codi6
codi6

Reputation: 566

Making a button not appear if an aria-label appears within the div

Within this else statement, I'm trying to say that if there is an aria-label whose value is "Comment" within the uCW div, don't add the button. Otherwise, add the button. Nothing is appearing.

        else {
            if ($(uCW).find([aria-label=="Comment"]))
                {rollback}
            else
                {more.before(button);}
            }

This is the aria-label I'm trying to NOT add buttons to (it's inside uCW and within a lot of divs)

<div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1">

Upvotes: 0

Views: 43

Answers (3)

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can check inside uCW has label comment as

var exist = $(".uCW [aria-label='Comment']").length;
    if( exist == 0 )
    {
      $(".uCW").append("<button>Test</button>");
    }

var exist = $(".uCW [aria-label='Comment']").length;
if( exist == 0 )
{
  $(".uCW").append("<button>Test</button>");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="uCW">
<div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1">
</div>

Upvotes: 0

Felippe Regazio
Felippe Regazio

Reputation: 389

You are using == instead the = on a selector. Also you must add quotes in your find selectors. You can do the following in Jquery:

$('.your-div').find('[aria-label="Comment"]');

Then, you can check if they were found using:

if ($('.your-div').find('[aria-label="Comment"]').length > 0) {
  // aria label = Comment found
} else {
  // ...
};

If you assign this condition to a const, your code can be more semantic:

const ariaLabelComment = $('.your-div').find('[aria-label="Comment"]').length > 0;

if (ariaLabelComment) {
  // aria label = Comment found
} else {
  // ...
};

In Pure javasript

const uCW = document.querySelector('.your-ucw-div-selector');

const ariaLabelComment = uCw.querySelectorAll('[aria-label="Comment"]').length > 0;

if (ariaLabelComment) {
  // found
} else {
  // ...
}

Upvotes: 0

Joel Hager
Joel Hager

Reputation: 3440

You have to access the attribute using a string. Like this $("[meta-tag='value']");

I'll amend my answer when you post code that will be more specific. :)

const aria = $("[aria-label='Comment']");

console.log(aria);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1">
</div>

<div class="not"></div>

Upvotes: 0

Related Questions