Steps for organizing a list based on the number of clicks

I've created an HTML list with images of political party logos. Each logo is a grid item that can be clicked on. I want to sort the list based on the number of clicks each logo receives, essentially ranking them by popularity. However, I'm not sure how to write the JavaScript code to accomplish this task as I have limited experience with JavaScript.

If anyone has expertise in JavaScript and can provide guidance or assistance in creating the sorting functionality, it would be greatly appreciated. I've attempted a few approaches myself, but so far none have been successful.

Answer №1

Want to organize a list using CSS? Here's how you can do it 🤣 :

function adjustOrder(event) {
  let li = event.target.closest('.order-me')
  if (li) li.style.order -= 1;
}
document.querySelector('.grid-container').onclick = adjustOrder
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  align-self: center;
  justify-self: center;
  margin: auto;
}

.grid-item {
  align-self: center;
  justify-self: center;
  text-align: center;
}

.grid-item::after{
  content: 'style="' attr(style) '"';
  display: block;
}
<ul class="grid-container" onclick="adjustOrder">
    <li class="grid-item order-me">50plus</li>
    <li class="grid-item order-me">bbb </li>
    <li class="grid-item order-me">bvnl</li>
    <li class="grid-item order-me">cda</li>
    <li class="grid-item order-me">PVV</li>
    <li class="grid-item order-me">christenunie</li>
    <li class="grid-item order-me">D66</li>
    <li class="grid-item order-me">Denk</li>
    <li class="grid-item order-me">FVD</li>
    <li class="grid-item order-me">JA21</li>
    <li class="grid-item order-me">NSC</li>
    <li class="grid-item order-me">PVDAGROENLINKS</li>
    <li class="grid-item order-me">PVDD</li>
    <li class="grid-item order-me">SGP</li>
    <li class="grid-item order-me">SP</li>
    <li class="grid-item order-me">Volt</li>
    <li class="grid-item order-me">VVD</li>
</ul>

Answer №2

As pointed out by @Barmer;, one way to achieve your goal is by utilizing the data-clicks attribute. I have made some adjustments to your code. Here's an example:

const listItems = document.querySelectorAll('.grid-item');
const list = document.getElementById('Partijensort');

list.addEventListener('click', function(e) {
    if (e.target.classList.contains('grid-item')) {
        let clicks = event.target.getAttribute('data-clicks') || 0;
        event.target.setAttribute('data-clicks', ++clicks);
        sortListItems();
    }
});

function sortListItems() {
  const listItemsArr = Array.from(listItems);
  listItemsArr.sort(function(a, b) {
    const clicksA = a.getAttribute('data-clicks') || 0;
    const clicksB = b.getAttribute('data-clicks') || 0;
    return clicksB - clicksA;
  });
  listItemsArr.forEach(function(listItem) {
    list.appendChild(listItem);
  });
}
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  align-self: center;
  justify-self: center;
  width: 80vw;
  margin: auto;
}

.grid-item {
  align-self: center;
  justify-self: center;
  font-size: 1em;
  text-align: center;
  height: 10vw;
  width: 10vw;
  margin-top: 1vw;
  filter: drop-shadow(10px 10px 10px rgba(0, 0, 0, 0.25));
}
<ul id="Partijensort" class="grid-container">
  <li data-clicks="0" class="grid-item" id="plus">50plus</li>
  <li data-clicks="0" class="grid-item" id="bbb">bbb </li>
  <li data-clicks="0" class="grid-item" id="bvnl">bvnl</li>
  <li data-clicks="0" class="grid-item" id="cda">cda</li>
  <li data-clicks="0" class="grid-item" id="pvv">PVV</li>
</ul>

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

What is the method for calling a function in a JavaScript file?

I am facing a challenge with reinitializing a JavaScript file named AppForm.js after a successful ajax post response. Within the file, there are various components: (function(namespace, $) { "use strict"; var AppForm = function() { // Cr ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

The edit functionality in jqGrid does not function properly if custom search parameters are designated

Using the Guriddo jqGrid JS version 5.2.0 implemented here: @license Guriddo jqGrid JS - v5.2.0 - 2016-11-27 Copyright(c) 2008, Tony Tomov, [email protected] The code block below showcases an entire self-contained implementation of jqGrid. It inclu ...

Chrome on IOS: Experiencing screen freezing while scrolling

1) I'm working with Material UI in React JS. I have added a simple scrollable code, but when I reach the bottom of the screen/scroll, it freezes. 2) I also tried it with a simple HTML page and it worked correctly. <div style={{ ...

The navigation menu that usually appears on mobile devices is surfacing on desktop screens instead - update: images now

I have recently been working on my website juniorgoldreport.com, which was created using WordPress. The plugin I am using specifically for the mobile nav bar is The functionality and ease of use of this plugin are incredible. The setting to show the toolb ...

Tips for setting a Bootstrap 3 dropdown menu to automatically open when located within a collapsed navbar

Is there a way to have my dropdown menu automatically open when the collapsed navbar is opened? Take a look at this example I created in a fiddle to see what I'm working with so far. Right now, when you click on the navbar in its collapsed state, two ...

Problem with fetching Grails

I have a table nested within an anchor tag. The table is centered on the page, with each row containing an image. When the page loads, I noticed the following: a) The table initially appears at the top-left corner of the screen. b) As there are multiple v ...

Creating a Vue.js component that integrates a Bl.ocks.org graph

I'm a rookie in the world of D3 and I am trying to implement this cool d3 element into my Vue.js component. However, I've encountered an issue with the periodic rotation that I require not functioning properly. It seems to work initially but then ...

What causes the jQuery mouseenter events to be activated in a random sequence?

I currently have a setup of 3 nested divs, resembling the concept of a Matryoshka doll. Each div has a mouseenter event function bound to it. When moving the mouse slowly from the bottom and entering layer three, the events occur in the following sequence ...

Tips for improving the styling of a django form with mixed elements

Here are two different forms I am working with: class InitialForm(Form): trn = IntegerField(widget=NumberInput, required = False) klient = ChoiceField(choices=KLIENTS, required = False) class SecondForm(Form): faktura = CharField(max_length = ...

Is it possible to assign a specific value to a row within a table?

For each row (tr) generated from my database, I would like to assign a unique row value. Let's consider the table being created: while ($row = $database->fetch_array($result)) { $i=1-$i; $class = "row".$i; echo "<tr class='{$class} produ ...

Deliver data in batches of ten when the endpoint is accessed

I am currently in the process of developing a web application using Next.JS and Node. As part of this project, I have created my own API with Node that is being requested by Next.JS. One particular endpoint within my API sends data to the front end as an ...

What is the method for utilizing the onmouseover function to emphasize a specific part of an SVG graphic?

I am attempting to create an interactive SVG map of Europe. Each country is represented by a path element, and I want the opacity of a country to change to 0.5 when it is hovered over. Despite trying to define a JavaScript function for this purpose, noth ...

Sending form array information using jQuery AJAX

My form allows for the addition of multiple listings simultaneously. Additionally, there is a requirement to pass through an md5check. For example: <select name="master_id"></select> <select name="id2[]"></select> <select nam ...

A guide on transforming a JSON object representing an HTML element into a live HTML element for display on a webpage using C#, JavaScript, or jQuery

I am looking to retrieve a JSON object from a database and convert it into HTML format. Here is an example of the JSON structure: {"input":{ "name":"fname", "type":"text", "placeholder":"Ente ...

ng-grid defines different cellClass based on the value of the field

I am currently using the ng-grid and I want the column to display in a different color based on different values. I have tried, but not succeeded so far... $scope.gridOptions = { ........ columnDefs: [ { field: "status", displayName: "St ...

Error: JQuery's on() function is not defined

After modifying the code in the original and changed code boxes, I'm now encountering an Uncaught Type error: Undefined is not a function. Any thoughts on why this might be happening? Thanks Original: $('.comment').click(function(e){ ...

CSS: Handling Background Images Responsively

I'm experiencing an unusual issue where the image is not appearing, even though the div box shows up when I highlight elements on the page. Validation and inspection also show no issues. Here is the code: <div class="mainImage"></div> A ...

Sending a Variable and List to a Controller using Ajax

I am having an issue with passing data from a Text Box and a Select Options Multiple using knockout selectedOptions in a viewModel to my Controller via ajax. I am unable to receive the MetricsChosenModel information. var MetricsChosenModel= wi ...

Issue with Express.js: "opencv" module not found within a Docker container

Currently, I am in the process of setting up the OpenCV bindings for NODE to enable AI functionality on my express server. For this purpose, I am utilizing the Peter Braden Library - https://github.com/peterbraden/node-opencv. However, I am encountering a ...