Home > Software engineering >  How to match elements which have certain class but that are also not children of specific element?
How to match elements which have certain class but that are also not children of specific element?

Time:12-20

I want to match all drop-down items that have specific class, but only if that class don't have specific parent (by class as well).

Currently I found a way to only match those drop-down items that do not have the first class:

$("a.dropdown-item").not(".some_class");

But I need to match the not() part for elements whose parent dropdown div doesn't have the unique_dropdown as well

I tried the following but that didn't work:

$("a.dropdown-item").not("some_class", $(this).closest(".unique_dropdown"));

and

$("a.dropdown-item").not("some_class", $("a.dropdown-item").closest(".unique_dropdown"));

The html:

<div >
  <div >
    <ul>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
    </ul>
  </div>
</div>

<div >
  <div >
    <ul>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
    </ul>
  </div>
</div>


<div >
  <div >
    <ul>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
      <li>
        <a ></a>
      </li>
    </ul>
  </div>
</div>

In the above example, I only want to match <a> elements with some_class that are children of the .dropdown div without .unique_dropdown class

CodePudding user response:

Not sure that I understood well, but I hope this does the trick

$(".dropdown:not(.unique_class) a.dropdown-item:not(.some_class)") 
  • Related