Combine multiple elements by class in jQuery

Leo Messi :

I want to get the numeric values from some fields and add their value to a sum and put that sum value into another element.

It works nice if they are separate lines:

   var quantity0 = parseFloat($('#0').find('.cart-quantity-input').val());
   var quantity1 = parseFloat($('#1').find('.cart-quantity-input').val());
   var quantity2 = parseFloat($('#2').find('.cart-quantity-input').val());

   var totalQuantity = quantity0 + quantity1 + quantity2;
   $('.items-number').text(totalQuantity + ' items');

But when they are combined there must be something that I miss because it doesn't work:

 const totalQuantity = [...$('.cart-items .cart-quantity-input')]
    .map((subtotalElm) => Number(subtotalElm.textContent.val()))
    .reduce((a, b) => a + b, 0);
  $('.items-number').text(totalQuantity + ' items');

There is one difference, instead of taking the ids (0,1,2) it takes the parent class cart-items but the error is not from there.

trincot :

input elements have no textContent property, and chaining a .val() method will produce an exception.

As a side note: with $.map you avoid one extra iteration, and you can use unary + instead of Number

So:

const totalQuantity = $.map($('.cart-items .cart-quantity-input'), input => +input.value)
                       .reduce((a, b) => a + b, 0);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combine multiple functions in jquery

Combine multiple elements and append result to a div

How to check multiple elements has same class using jquery?

on click of multiple elements in jquery?

affect only the clicked element in jquery for multiple elements with same class

jQuery .text() multiple elements same class

Jquery Remove Class Multiple Elements

jQuery Check if multiple elements have a class

Combine multiple simple jQuery functions

Change class of multiple elements

How to combine multiple onclick events/elements

Combine multiple class template specializations

combine multiple array elements into single object in javascript

How can I add a conditional class to multiple elements using jQuery?

Add and Remove Class from multiple elements with jQuery

Multiple elements with same class

Jquery matching multiple elements with same id/class

jQuery toggle multiple elements

Jquery hovering multiple elements

Is there a way I can combine multiple collapsible elements one jquery function?

jQuery/Javascript get classes of multiple elements to show an element with this class

Remove class from multiple elements in JavaScript or jQuery

Jquery class for list elements

How to combine multiple find() in jquery?

Set html contents of an element using jQuery but with multiple elements of the same class

Combine elements out of multiple lists?

Same class name for multiple elements but execute event independently in pure jQuery

combine multiple elements in a list with different indexes in r

jQuery combine variable with pseudo class