Tally Every Number Encountered Within the Blog Archive

I've been facing some challenges putting together this jquery functionality. My original idea was to create a toggle effect for the archive's years and months. For example, when I click on a YEAR, it should toggle down to display all MONTHS, and when I click on a MONTH, it should toggle down to show the links below. The toggle action should be specific to the clicked link.

However, the issue I encountered was with the formatting of the jquery script. I intended for the years and months to trigger a "slideDown" or toggle effect, but when I clicked on a month, it toggled all the months instead of just one.

If it helps, you can access the code on JSFiddle here.

https://i.sstatic.net/pDyKS.jpg

Thank you in advance.

$('.years span').click(function() {
$('.years').addClass('active'); 
$('.months').show(); }); 

$('.months span').click(function() { 
$('.months').toggleClass('active'); 
$('.months').find('ul').slideToggle(); }); 

$('.months a').click(function() { 
$('.months').addClass('active'); 
$('.months ul').show(); });
.year-name p{
display: inline;

}
.year-name {
  display: inline;
}

.month-name p{
display: inline;

}
.month-name {
  display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="archive-posts">
<ul>

<div class="archive-post y2019"><span class="year-name">2019 <p></p></span>
<ul>
<li class="month month-10"><span class="month-name">October <p></p>
</span>
<a href="">Return</a>
</li>

<li class="month month-9"><span class="month-name">September <p></p>
</span>
<a href="">Help</a>
<a href="">Hello</a>
</li>
</ul>
</div>

<br><br>

<div class="archive-post y2018"><span class="year-name">2018 <p></p></span>
<ul>
<li class="month month_9"><span class="month-name">July <p></p>
</span>
<a href="">A Kind Gesture</a>
</li>
</ul>
</div>

</ul>
</div>

Answer №1

Utilize the .each() method to iterate through each year and month, calculating the number of items within that element.

$("#archive-posts .archive-post").each(function() {
  var y = $(this).find("li.month").length;
  $(this).find(".year-name p").text("(" + y + ")");
});

$("#archive-posts li.month").each(function() {
  var m = $(this).find("a").length;
  $(this).find(".month-name p").text("(" + m + ")");
});
.year-name p, .year-name, .month-name, .month-name p {
  display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="archive-posts">
  <ul>

    <div class="archive-post y2019"><span class="year-name">2019 <p></p></span>
      <ul>
        <li class="month month-10"><span class="month-name">October <p></p>
</span>
          <a href="">Return</a>
        </li>

        <li class="month month-9"><span class="month-name">September <p></p>
</span>
          <a href="">Help</a>
          <a href="">Hello</a>
        </li>
      </ul>
    </div>

    <br><br>

    <div class="archive-post y2018"><span class="year-name">2018 <p></p></span>
      <ul>
        <li class="month month_9"><span class="month-name">July <p></p>
</span>
          <a href="">A Kind Gesture In Return</a>
        </li>
      </ul>
    </div>

  </ul>
</div>

By the way, your HTML is not valid. <p> should not be placed inside <span>.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The button with the class ".navbar-toggler" in Bootstrap 4 is not designed to float to the right

I've been trying to use Bootstrap 4, but no matter what I do, I can't seem to get the .navbar-toggler button to align to the right. It feels like I'm missing something simple, but I just can't figure out what's going wrong. Check ...

Enhancing User Experience with JQuery AutoComplete for ASP.NET with Multiple Fields Receiving

I'm currently working on implementing the AutoComplete functionality of JQuery to update multiple fields on an ASP.NET website. While I've successfully managed to get a single textbox to work, I'm struggling with updating multiple textboxes ...

Personalize the style of ordered lists

Greetings! I am attempting to customize the style of an ordered list like the one shown in the image on another website. https://i.sstatic.net/BQok4.png However, instead of the circle used in that picture, I want to use a different CSS Image like the one ...

Interacting with jQuery mouse events on elements below the dragged image

I'm attempting to create a drag-and-drop feature for images using jQuery. While dragging, I generate a thumbnail image that follows the mouse cursor. However, this is causing issues with detecting mouseenter and mouseleave events on the drop target pa ...

Using Backbone.sync to customize dataType

I am working on an application that utilizes a .CSV file as the primary data source for a Backbone Model. I am interested in finding the most effective approach to changing the sync method so that it uses dataType "text" instead of "json". Any insights on ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

The charAt function in a basic JavaScript if statement is failing to execute

When a user inputs a number that does not start with 6 or 9, an error occurs: console.log($(this).val().charAt(0)); if($(this).val().charAt(0) != 6 || $(this).val().charAt(0) != 9){ x=false; }else { x=true; } The console.log function corre ...

Identifying Changes in Form Values Using jQuery

I am facing a challenge with a dynamic form that needs to detect the field sequence properly. Below is my JavaScript file: var i = 1; $("a.add-line").click(function(){ $("div.items").append( $('<div>').attr('id',&ap ...

Trouble with posting a form submission after populating a select box dynamically using AJAX

Trying my hand at chaining select boxes in a web form using ajax for the first time and feeling a bit lost. The issue I'm facing is that when a user selects a country from one select box, an ajax request retrieves options for states and territories in ...

Div - containing images removed from slideshow

Currently, I am in the process of constructing a small website that includes a slideshow beneath my navigation bar. My aim is to utilize flexbox while making the slideshow "responsive." By this, I mean that when altering the dimensions of my browser window ...

Are there any risks associated with using nested setTimeout functions with the same name?

While reviewing the source code of typed.js, I noticed that the main function in this plugin utilizes a design pattern with multiple setTimeout functions nested inside one another. Here is a snippet of the code: self.timeout = setTimeout(function() { / ...

AlpineJS causes jQuery & Bootstrap to fail to load after being loaded

Everything was smooth sailing with my bootstrap, livewire, and jQuery before I introduced AlpineJS. But now, the browser is constantly throwing Uncaught TypeErrors at me. I even tried tweaking the script to load AlpineJS alongside the other libraries, but ...

Innovative ways to design a responsive carousel with Bootstrap

My goal was to create a slider with divisions inside a bootsrap carousel, and here is what I did. However, I am looking for guidance on how to adjust it so that on screens smaller than sm (of bootstrap), there are only two divisions in one carousel, and a ...

By employing the $watch method, the table disappears from the div element

I've integrated the isteven-multi-select directive for my multi-select dropdown functionality. By providing it with a list of thingsList, it generates a corresponding checkedList as I make selections. Initially, I used a button to confirm the selecti ...

Having trouble submitting ajax form data through nodemailer

Hey there, Recently, I created a web app using node.js and express. Everything seems to be working fine except for one issue - I am struggling to get the JSON data sent by AJAX into Nodemailer. Despite confirming that my AJAX is successfully sending the ...

What could be causing my image not to show up on ReactJS?

I'm new to ReactJS and I am trying to display a simple image on my practice web app, but it's not showing up. I thought my code was correct, but apparently not. Below is the content of my index.html file: <!DOCTYPE html> <html> & ...

Looking to replace an existing class with a new one in a div element?

I have developed a form builder where selecting a value option from 1 to 12 will append a col-xs-(1-12) class to a div. However, I am facing an issue where choosing a different option does not remove the previously added class. function addGrid() { ...

Animated SVG Arrow Design

I created a dynamic SVG animation that grows as you hover over it. Since I'm still learning about SVG animations, I encountered some issues with my implementation. The animation is quite straightforward - when hovering over the SVG arrow, the line sho ...

Implementing Jquery: Show a class after clicking the button and then remove it

I am looking for a way to display a class when a button is clicked and then remove it when the same button is clicked again, repeatedly. I have multiple buttons so I cannot use the toggle function as it does not provide the desired behavior. I have tried v ...

Tips for achieving a standard dropdown appearance in a collapsed navbar with Bootstrap split button dropdown

Trying to create a navbar with a split dropdown button has been a challenge. The design either goes awry when the screen is narrow and the navbar is hidden behind the toggle, or it doesn't look right on a wider screen when the navbar is expanded. Cur ...