0

I'm trying to get the data from the td cells when the checkbox is checked in the same row in an object to pass it to other page but I get /n in return..

productOrder.ejs

<table id="review-products">
  <thead>
    <tr>
      <th ></th>
      <th>Product Name</th>
      <th>Price </th>
      <th>Quantity</th>
    </tr>
  </thead>
  <tbody>
    <% products.forEach((products) => { %>
    <tr id="order-item-510" class="tst-orderItemRow">
      <td>
          <input type="checkbox"  value="<%= products.productId %>"  id="check" >
        </td>
      <td class="name" >
          <%= products.productName %>
      </td>
      <td class="currency" >
           <%= products.price %>
      </td>
      <td class="quant">
          <input type="text" class="foo" value="" >
      </td>
    </tr>
    <% }) %>
  </tbody>
</table>
    <button  id="save" name="button">Save</button> 

My ajax code:

$("#review-products input[type=checkbox]:checked").each(function() {
  var row = $(this).closest("tr");
  var message0 = row.find('#check').val();
  var message1 = row.find('.name').text();
  var message2 = row.find('.currency').text();
  var message3 = row.find(".foo").val();
  var result = {
    check: message0,
    name: message1,
    p: message2,
    q: message3,
    total: message2 * message3
  }
  console.log(result);
});

and in the console I got this: {check: "1", name: "↵ ball↵ ", p: "↵ 10↵ ", q: "3", total: 30}

leonheess
  • 16,068
  • 14
  • 77
  • 112
Eman Emad
  • 77
  • 1
  • 4
  • 13

1 Answers1

0

trim your value

$("#review-products input[type=checkbox]:checked").each(function() {
  let row = $(this).closest("tr");
  let message0 = row.find('#check').val();
  let message1 = row.find('.name').text().trim();
  let message2 = row.find('.currency').text().trim();
  let message3 = row.find(".foo").val().trim();
  let result = {
    check: message0,
    name: message1,
    p: message2,
    q: message3,
    total: message2 * message3
  }
  console.log(result);
});
leonheess
  • 16,068
  • 14
  • 77
  • 112
sarvon ks
  • 626
  • 3
  • 10