Home > Mobile >  how could I modify cell data used same class for column all class in column have same class
how could I modify cell data used same class for column all class in column have same class

Time:10-08

enter image description here

I want to change table cell data using jquery I want it to be replaced by data that will be taken from the same cell. eg - older cell data - October 5, 2021, 18:00, I want it to change to only 18:00. All the cells have come to class.

I have tried

var str1 = $('.service_date').text();
              console.log(str1);
             $.each(str1 , function() { 
              var subStr = str1.substring(str1.length, str1.length-5);
              console.log(subStr);});

CodePudding user response:

You're just setting str1 to the text of the first matching element, not an array of all of them.

Use the .text() method with a function argument. This will iterate over all the matching elements, replacing their text with the return value of the function.

$("#update").click(function() {
  $('.service_date').text(function(i, str1) {
    return str1.slice(-5);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Pickup Time</th>
  </tr>
  <tr>
    <td class="service_date">October 5, 2021, 18:00</td>
  </tr>
  <tr>
    <td class="service_date">December 10, 2021, 18:00</td>
  </tr>
</table>
<button id="update">Remove dates</button>

  • Related