Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total number of elements decrease automatically by one when it is clicked in jQuery

Tags:

jquery

I am trying to have a counter showing the total number of tasks that are aren't complete. The complete ones are marked by line-through when clicked. When i click on each list the total number also should reduce by 1. I created a variable representing the total number - 1 but the total number only reduce once only at first click. Please i am stuck at these and i am still new on jQuery.

This is my code

$(document).ready(function() {
  $('button').on('click', function() {
    // Create variable to represent the value
    var $value = $('input').val()

    // Putting the values as list in web browser using if and else.
    if ($value === "") {
      alert("Enter your to do list")
    } else {
      $('.list').append("<li>" + $value + "</li>")
    }

    //Creating variable that will hold totoal number of list
    var $count = $('li').length;

    // Creating variable to repersent list subtraction
    var $sub = $count - 1;
    var $solution = $sub--;

    // Underline the list when cliked by user
    $('li').on("click", function() {
      $(this).css('text-decoration', 'line-through')

      $('.text').text("You have " + $solution + " uncompleted tasks")
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title>J-query Test</title>
  <style>
    h2 {
      text-align: center;
    }
    
    body {
      margin: 0;
      padding: 15px 0 0 15px;
      border-collapse: none;
    }
    
    li {
      font-size: 20px;
      font-family: 'candara';
    }
  </style>
</head>

<body>
  <p>Write down your to do list</p>
  <input type="text" name="list">
  <button>Click</button>
  <ul class="list">
    <li>To do list</li>
    <li>To do list</li>
    <li>To do list</li>
    <li>To do list</li>
  </ul>
  <div class="text">

  </div>
</body>

</html>
like image 555
Tukei David Avatar asked Nov 08 '22 03:11

Tukei David


1 Answers

You can play with a class line to achieve what you need. Avoid nested click events, check the example:

$(document).ready(function() {

    var $count;
    var $sub;
  
    $('button').on('click', function() {
        // Create variable to represent the value
        var $value = $('input').val()

        // Putting the values as list in web browser using if and else.
        if ($value === "") {
            alert("Enter your to do list")
        } else {
            $('.list').append("<li>" + $value + "</li>")
        }
        $count = $count + 1;
        $('.text').text("You have " + $count + " uncompleted tasks")
    });


    // Underline the list when cliked by user
    $(document).on("click", "li:not(.line)", function() {

        $count = $('li:not(.line)').length;
        $(this).addClass("line")
        $count = $count - 1;

        $('.text').text("You have " + $count + " uncompleted tasks")
    });


});
.line{
  text-decoration: line-through;
}

 h2 {
    text-align: center;
}

body {
    margin: 0;
    padding: 15px 0 0 15px;
    border-collapse: none;
}

li {
    font-size: 20px;
    font-family: 'candara';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <title>J-query Test</title>
    <script type="text/javascript" src="js/jqueryuncomp-3.3.1.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
</head>
<body>
    <p>Write down your to do list</p>
    <input type="text" name="list">
    <button>Click</button>
    <ul class="list">
        <li>To do list</li>
        <li>To do list</li>
        <li>To do list</li>
        <li>To do list</li>
    </ul>
    <div class="text">

    </div>
</body>
</html>
like image 184
SilverSurfer Avatar answered Nov 15 '22 07:11

SilverSurfer