0

I have this html code here:

  <div class="container">
    <section>
      <header class="date">May 2014</header>
      <article>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, dolorem, laborum non illum voluptate vitae quibusdam impedit</article>
      <article>Repellendus, dolorem, laborum non illum voluptate vitae quibusdam impedit aperiam placeat minus ratione mollitia expedita tempore reprehenderit maxime.</article>
      <article>ratione mollitia expedita tempore reprehenderit maxime.</article>
      <a href="#">Remove Article</a>
    </section>

    <section>
      <header class="date">March 2014</header>
      <article>Repellendus, dolorem, laborum non illum voluptate vitae quibusdam impedit aperiam placeat minus ratione mollitia expedita tempore reprehenderit maxime unde quas beatae maiores.
      </article>
      <a href="#">Remove Article</a>
    </section>

    <section>
      <header class="date">April 2014</header>
      <article>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</article>
      <article>Repellendus, dolorem, laborum non illum voluptate vitae quibusdam impedit aperiam placeat minus ratione mollitia expedita tempore reprehenderit maxime unde quas beatae maiores.
      </article>
      <a href="#">Remove Article</a>
    </section>
  </div>

When I click on 'Remove Article" link i should remove one of the article above it. I've got that.

Now what i'm trying to accomplish here is, when I remove all articles 'one by one' that belongs to specific section I need automaticly to have the header 'date' and the link 'Remove Article' to be removed completely.

I have tried different ways, but still I can see both header and link, but when I refresh page they are gone.

1
  • 2
    Post your JavaScript please. Commented May 9, 2014 at 20:34

4 Answers 4

2

I think I got you. Do you want to remove articles one by one then if there's no more remove the whole section? If so, this should work:

$('a').on('click', function (e) {
  e.preventDefault();

  var $section = $(this).closest('section'),
      $articles = $section.find('article');

  $articles.last().remove();


  // $articles.length will not be one less just because we removed 
  // an item, so just checking for 1 here is the same as checking
  // if it's empty.

  if ($articles.length === 1) {
    $section.remove();
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

yes, that's what I was looking for. Each time i click on 'Remove Article' it deletes one. When I delete last article it deletes the whole section which include Header and link. Before I asked the question, I had the part done, i was missing [ last().remove ]. Thanks
1

have you tried binding this.parentElement.remove() to the a elements in your html? It will remove the entire section.

$('.container a').click(function(){
     this.parentNode.remove()
}

4 Comments

This would be better as a comment since it does not answer the OP's question.
note this currently is not supported in any version of IE
fine. i'll change it to node
Why? you are using jquery so use the parent() function!
1
$('section a').on('click', function(e) {
    e.preventDefault();
    var self = $(this);
    var articles = self.siblings('article');
    if (articles.length > 1) {
        articles.last().remove();
    }
    else {
        self.parent().remove();
    }
})

JSFiddle

Comments

0

UPDATE: removes each article until only one is left in a section, then removes last article and section. Thanks @Barmar for pointing out my error.

$('body').on('click', '.container a', function (e) {
    e.preventDefault();
    var articleLength = $(this).closest('section').find('article').length;
    if (1 != articleLength) {
        $(this).closest('section').find('article:last').remove();
    } else {
        $(this).closest('section').remove();
    }
});

1 Comment

This will remove the section even if there are still other articles in it. He only wants to remove the section when all articles have been removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.