Table Traversing with Jquery

419
November 23, 2016, at 2:55 PM

I have a cart page where the quantity is not the correct quantity as the products are sold in bulk (1, 10, 100 items).

What I am trying to achieve is getting the number of items in the pack, multiply by the quantity and display the total number of items.

Here's a JSFiddle and the code:

jQuery(document).ready(function($){
$( ".variant_title" ).each(function() {
  var str = $(this).text();
  var qty = $(this).nextUntil('input[id^=updates_]').val();
  console.log(qty);
  if ( str.indexOf('jackets') > -1 ) {
      unities = str.slice(18,21);
      if (unities === '100' || unities === '10 ') {
        console.log(unities);
        totalUnities = unities * qty;
        console.log(totalUnities); 
        $(this).append('<br />' + totalUnities + ' Unities');
      }
  } else {
      unities = str.slice(18,19);
      if (unities === '1') {
        console.log(unities);
        totalUnities = unities * qty;
        $(this).append('<br />' + totalUnities + ' Unity');
      }
  }
});
});

Table example

My problem now is traversing the DOM and getting the value of the input field next to the description and multiplying.

Does anyone know where am I making a mistake here?

Thanks

Answer 1
  var str = $(this).text();
  var tr = $(this).closest('tr');  
  var qty = $(tr).find('input').val();
  console.log(qty);

Yo need to get your parent element (tr here) and find the input to get your quantity. hope this help

Answer 2

nextUntil(-selector-) method selects all prior elements to -selector-
use this approach to get quantity:

var str = $(this).text();
var qty = $(this).parents('.item').next('td.qty').find("input[id^='updates_']").val();

http://jsfiddle.net/eetyr6ae/3/

READ ALSO
Text coming out of paragraph tag?

Text coming out of paragraph tag?

I'm using masonry layout for my website and I'm generating the boxes from php. whenever i have a lot of text on one of the p tags or the box, the text overflows out of the tag.

457
Why are my table rows being duplicated?

Why are my table rows being duplicated?

I am generating a table using jQuery and loading its rows from a data structure. .

215
PhantomJS Hangs on Page Errors

PhantomJS Hangs on Page Errors

I am using PhantomJS 2 and the latest CasperJS to test a remote webpage containing some TypeErrors. While logging into the webapp, a popup.

388
How do I call a function with jquery for IE?

How do I call a function with jquery for IE?

I'm having a slight issue with jquery on IE. .

347