0

var sum = 0;
$(document).on("input", ".payments", function() {
  var amount = 100;
  var values = [];
  $('input').each(function(index, element) {
    values.push(parseInt($(element).val(), 10) || 0);
  });

  var total = values.reduce(function(sum, val) {
    return sum + val;
  }, 0);
  var diff = amount - total;
  if ($(this).is(':last-child')) {
    if (diff > 0) {
      console.log("last not ok")
    } else {
      console.log("last ok")
    }
  } else {
    if (diff >= 0) {
      console.log("ok")
    } else {
      console.log("not ok")
    }
  }


})
input{display:block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />

In the demo when I input in the box and reached the last box it does not go in the condition of last box.

I want to be able to tell if the one i'm inputting on(typing on) is the last box of the class payment.

3
  • want to tell ? what it means exactly. your question is not clear Commented Jan 4, 2017 at 6:34
  • not getting you properly please try this to select last element if you want using jquery $( ".payments:last-child" ) or for css .payments:last-child{} Commented Jan 4, 2017 at 6:34
  • i tried $( ".payments:last-child" ) but it didnt work. i want to know if the one I am inputing on is the last child of class payment Commented Jan 4, 2017 at 6:37

3 Answers 3

2

The reason for :last-child not working is because the input fields are not inside any tag hence they are not children to any parent. So there are two ways to solve this,

  1. put all the input fields inside a div element

  2. change the if condition to if($(this).is('input:last'))

Sign up to request clarification or add additional context in comments.

2 Comments

i tried using method why it didnt work my code is like this if($('.payments').last()){
$('.payments').last() returns an element so it will work when used like this if($(this).is($('.payments').last()))
1

Try this changes

var sum = 0;
$(document).on("input", ".payments", function() {
  var amount = 100;
  var values = [];
  $('input').each(function(index, element) {
    values.push(parseInt($(element).val(), 10) || 0);
  });

  var total = values.reduce(function(sum, val) {
    return sum + val;
  }, 0);
  var diff = amount - total;
  if ($(this).is(':last-of-type')) { // I have changed this line
    if (diff > 0) {
      console.log("last not ok")
    } else {
      console.log("last ok")
    }
  } else {
    if (diff >= 0) {
      console.log("ok")
    } else {
      console.log("not ok")
    }
  }


})
input{display:block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />

I hope this will work

EDIT : $("input:last-of-type") - Select every <input> element that is the last <input> element of its parent:

For more details visit link

2 Comments

can add explanation on how it works and why current didnt work?
Check for difference between last-child and last-of-type, you will get it
0

var sum = 0;
$(document).on("input", ".payments", function() {
  var amount = 100;
  var values = [];
  $('input').each(function(index, element) {
    values.push(parseInt($(element).val(), 10) || 0);
  });

  var total = values.reduce(function(sum, val) {
    return sum + val;
  }, 0);
  var diff = amount - total;
  if ($(this).index() == ($(".payments").length)) {
    if (diff > 0) {
      console.log("last not ok")
    } else {
      console.log("last ok")
    }
  } else {
    if (diff >= 0) {
      console.log("ok")
    } else {
      console.log("not ok")
    }
  }


})
input{display:block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />
<input type="text" class="payments" />

Check for index is equal to class length

Comments

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.