0

I tried to make a code to remove a li from ul but I would like to be more specific. I would like to find exact anchor link before removing it along with the li it contains.

In the example below, I want to remove only Another link.

My code so far looks like:

<ul class="working-list">    
        <li class="work color1">
            <a href="?c=Some link 1">
                <span class="title">
                    link 1</span>
            </a>
        </li>
    
        <li class="work color1">
            <a href="?c=someLink2"><span class="title">
                    Some link 2</span>
            </a>
        </li>
    
        <li class="work color1">
            <a href="?c=anotherLink"><span class="title">
                    Another link</span>
            </a>
        </li> 

        <li class="work color1">
            <a href="?c=SmallLink"><span class="title">
                    Small link</span>
            </a>
        </li>    
    </ul>

And the script so far:

let pickEl = document.querySelectorAll('.working-list li');
    // let pickA = document.querySelector('a[href=\'?c=anotherLink\']');
    console.log(pickEl);
    pickEl[3].remove();

What would be the best approach?

onweb
  • 75
  • 7
  • 2
    `document.querySelector("a[href*='anotherLink']").closest("li").remove();`. Familiarize yourself with the [DOM API](//developer.mozilla.org/docs/Web/API/Document_Object_Model). – Sebastian Simon Nov 09 '21 at 10:24

2 Answers2

2

You can try using Attribute selectors selector:

let pickEl = document.querySelector('.working-list li a[href="?c=anotherLink"]');
console.log(pickEl);
//remove the parent li
pickEl.closest('li').remove();
<ul class="working-list">    
    <li class="work color1">
        <a href="?c=Some link 1">
          <span class="title">link 1</span>
        </a>
    </li>

    <li class="work color1">
        <a href="?c=someLink2">
          <span class="title">Some link 2</span>
        </a>
    </li>

    <li class="work color1">
        <a href="?c=anotherLink">
          <span class="title">Another link</span>
        </a>
    </li> 

    <li class="work color1">
        <a href="?c=SmallLink">
          <span class="title">Small link</span>
        </a>
    </li>    
</ul>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

You can forEach the pickEl then see textContent like:

let pickEl = document.querySelectorAll('.working-list li');
const deleteText = 'Another link';
pickEl.forEach(el =>{
  const a = el.querySelector('a');
  if(a.textContent.trim() === deleteText){
    el.remove();
  }
});
<ul class="working-list">
  <li class="work color1">
    <a href="?c=Some link 1">
      <span class="title">
                    link 1</span>
    </a>
  </li>

  <li class="work color1">
    <a href="?c=someLink2"><span class="title">
                    Some link 2</span>
            </a>
  </li>

  <li class="work color1">
    <a href="?c=anotherLink"><span class="title">
                    Another link</span>
            </a>
  </li>

  <li class="work color1">
    <a href="?c=SmallLink"><span class="title">
                    Small link</span>
            </a>
  </li>
  </ul>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34