Choose only the li elements that have been specifically clicked on

As a beginner, I am working on a project where I need to select the specific li element that is clicked using jQuery. However, I am facing an issue where clicking on one li selects all of them at once. Here is the link to my code snippet: JS FIDDLE

This is how my HTML looks:

var clicks = 0;
$("li").on("click", function () {
  clicks++;
  $('.top-right').html(clicks);
});
.list li {
  list-style-type: none;
  display: inline-block;
  background-color: white;
  margin-right: 7px;
}

.list {
  margin: 5px;
}

.list li a {
  text-decoration: none;
  color: black;
  padding: 2px;
}

.list li a:hover,
.list li a:hover {
  color: black;
}
<!DOCTYPE html>
<html>

<head>
  <title>Exam</title>
</head>

<body>
  <div class="list">
    <ul>
      <li>
        <a href="#">NAME
          <span class="line"></span> AWONUSI OLAJIDE</a>
        <span class="top-right"></span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> ADEGBULUGBE TIMILEHIN</a>
        <span class="top-right"></span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> OLUOLU ADEDEJI</a>
        <span class="top-right">9999</span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> OYAJE JOSHUA</a>
        <span class="top-right">9999</span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> ALABI OJO ADE</a>
        <span class="top-right">9999</span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> AKILO AWANI</a>
        <span class="top-right">9999</span>
      </li>
      <li>
        <a href="#">NAME
          <span class="line"></span> AYUBA DEJIMARK</a>
        <span class="top-right">9999</span>
      </li>
    </ul>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>

Answer №1

If you want to retrieve the element inside the clicked element, make use of the .find() method. Additionally, the content to be displayed should be one more than the current value present inside it.

$("li").on("click", function() {
  $(this).find('.top-right').html(function(){
    return +$(this).html() + 1
  });
});

$("li").on("click", function() {
  $(this).find('.top-right').html(function(){
    return +$(this).html() + 1
  });
});
.list li {
  list-style-type: none;
  display: inline-block;
  background-color: white;
  margin-right: 7px;
}

.list {
  margin: 5px;
}

.list li a {
  text-decoration: none;
  color: black;
  padding: 2px;
}

.list li a:hover,
.list li a:hover {
  color: black;
}
<!DOCTYPE html>
<html>

<head>
  <title>Exam</title>
</head>

<body>
  <div class="list">
    <ul>
      <li><a href="#">NAME   <span class="line"></span> AWONUSI OLAJIDE</a><span class="top-right"></span></li>
      <li><a href="#">NAME   <span class="line"></span> ADEGBULUGBE TIMILEHIN</a><span class="top-right"></span></li>
      <li><a href="#">NAME   <span class="line"></span> OLUOLU ADEDEJI</a><span class="top-right">9999</span></li>
      <li><a href="#">NAME   <span class="line"></span> OYAJE JOSHUA</a><span class="top-right">9999</span></li>
      <li><a href="#">NAME   <span class="line"></span> ALABI OJO ADE</a><span class="top-right">9999</span></li>
      <li><a href="#">NAME   <span class="line"></span> AKILO AWANI</a><span class="top-right">9999</span></li>
      <li><a href="#">NAME   <span class="line"></span> AYUBA DEJIMARK</a><span class="top-right">9999</span></li>
    </ul>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</body>

</html>

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

Steps to easily modify the color of Font Awesome stacked icons when hovering over them

I have incorporated two Font Awesome icons into my design: fa-circle-thin fa-user-plus These icons are stacked to create a circular icon appearance. <span class="fa-stack fa-sm"> <i class="fa fa-circle-thin fa-stack-2x"></i> <i c ...

Pause animation when hovering over their current positions

I am working on a project with two separate divs, each containing a circle and a smiley face. The innercircle1 div is currently rotating with a predefined animation. My goal is to create an effect where hovering over the innercircle1 div will pause its rot ...

Optimize Google Places Autocomplete: Customize the way search results are shown after selection (display only street name and number)

Struggling to update Google's autocomplete input value to only show the selected "streetname number" instead of the full address. It works when the user hits enter, but the value changes back when the input field loses focus. Looking for a simple sol ...

Learn the process of sending notifications upon clicking on an advertisement by a user

I'm currently working on a system that displays ads on mobile devices. Our clients have the option to use their own custom HTML code for their advertisements. We want to be able to track when a user clicks on these ads. I am considering wrapping the u ...

What is the best way to showcase the contents of an array of objects from JavaScript on an HTML page in a dynamic

Looking for guidance in Javascript as a beginner. For an assignment, I need to create two separate JS files and use them to display data dynamically on an HTML page. One JS file, named data.js, contains an array of objects representing a product catalog. T ...

Utilizing the CSS property "overflow:hidden;" to control the content visibility within nested div elements

Within a parent div, I have 6 nested divs with no border-radius set. The parent div has a border-radius applied, causing the corners of the child divs to spill over the rounded corners of the parent. Even setting overflow to hidden does not seem to solve t ...

What is the best way to showcase a bootstrap range slider and ensure the corresponding value is reflected in the input field?

I have implemented the bootstrap 5 range component, but I am encountering some issues Why is the dot not displaying in the center of the line even though I added the min value of 1? Is there a way to display the value of the range in the input field? How ...

Display the title tag when hovering over an image

Hey there! I'm looking to display the title tag of an image directly inside the image itself. Here's an example to illustrate what I mean: http://jsfiddle.net/51csqs0b/ .image { position:relative; width:200px; height:200px; } .ima ...

Remove a specific date entry from the database using VUE JS and Axios

I am encountering a challenge with Vuejs as I work on a To-Do list. The tasks need to be stored in a MySQL database, and I also want the ability to delete tasks. While I have succeeded in importing and creating data, I'm facing difficulty in deleting ...

What are the ways to prevent the slider from automatically moving?

I have a carousel/slider that is automatically moving, but I want to stop it so that it only moves when the navigation arrows are clicked. Here is a picture of the slider https://ibb.co/HPr9rJR. Currently, it is moving on its own to the left side, but I wo ...

What is the method for transforming a JavaScript array (without an object name) into JSON format (with an object name)?

Currently, I am using an ajax query to read a local csv file and then loading the extracted values into an array. This is how the string value appears in the csv file: "Tiger","Architect","800","DRP","5421" ...

Recurrence of solely the middle segment of the separator's background picture

I have a unique divider image with fading top and bottom parts. I'm wondering if it's possible to use a sprite and 3 divs to only repeat the middle section, considering that my height is variable. Do I need separate images for the top, middle, an ...

What is the reason behind the line-height adjustment only impacting the top portion of the font?

Currently, I am experimenting with the dot HTML character to create icons in different colors. However, I am encountering an issue with the line-height property. It appears that the line-height setting only affects the placement of the top part of the char ...

Submit the request when the fileReader's onload event is triggered

Do you have any suggestions on how to improve my fileReader for uploading images? I am currently facing an issue where multiple requests are being sent to the server when there are more than 1 image due to a for loop. How can I modify my code to address ...

Displaying Bootstrap Quotes in a vertical orientation on the screen

https://i.sstatic.net/90qYG.pngI'm struggling to make a long quote display wider horizontally and haven't been able to figure it out. Below are the current HTML and CSS scripts I have. I simply want the text to be shown wider than it currently is ...

Empty array returned when using wrapper.classes() with Vue-test-utils and CSS modules

Recently I started using Vue Test Utils along with CSS Modules. My Vue component is all set up with CSS Modules and it functions perfectly in the app as well as in Storybook. Take my BasicButton for example: <template> <button :class="[ ...

Utilizing jQuery UI datepicker as a fallback in absence of HTML5 native date input type

Although <input type="date"> is not commonly used for desktop, it is the preferred method for mobile devices. I am attempting to incorporate the fallback code located at the end of this section: <form> <input type="date"> </form&g ...

When developing a website in Sitecore 8, the link consistently opens in a new window rather than a new tab, even after including the target="_blank" attribute

When using Sitecore-8 CMS, external links are set to open in a new window instead of a new tab in Internet Explorer 10 and IE 9, even when target="_blank" is included in the href tag. We are seeking a solution to address this issue. Any assistance would b ...

Embarking on the journey of creating mobile web applications - where should you begin?

How can I begin learning to create mobile web apps? I have a keen interest in the design aspects, particularly the effects and animations that give them a native appearance. I suspect there are differences in CSS, HTML, and possibly JavaScript compared to ...

What is the best way to pinpoint the initial and final elements in a row of variable entries within a grid?

These are the tools currently in use: Vuejs Jquery Packery with masonry layout The project involves creating a page that pulls content from JSON and fills the homepage with posts, products, and pages in a single loop. Each object in the loop is arranged ...