1

I have the following code:

var sum = 0;
    $("#scLD > tbody > tr > td:nth-child(2)").each(function () {
        var x = $(this);
        if (x.parent().hasClass(/^onHandTdmm([0-9]+)$/)) {
            var y = parseFloat(x.text(), 10) || 0;
            sum += y;
        }
        if (x.parent().hasClass("onHandGrand")) {
            x.html(sum);
            sum = 0;
        }
    })

I am trying to search through a column and if any of the cells parent(the row) contains a certain class name followed by any single number to add it. So basically if the parent contains the class of onHandTdmm followed by any number (example onHandTdmm1). The regex I came up with (/^onHandTdmm([0-9]+)$/) just returns 0 in my function.

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
sm1l3y
  • 420
  • 2
  • 5
  • 15
  • Not sure that you can use regex in hasClass() method? But: http://stackoverflow.com/questions/13878759/jquery-match-part-of-class-with-hasclass – sinisake Dec 14 '16 at 20:10
  • Do you have classes that begin with `onHandTdmm` that don't have a number after it? You could just use the selector `[class*=onHandTdmm]` which looks for that string anywhere in the class. – Barmar Dec 14 '16 at 21:11

4 Answers4

1
  1. you need to escape: \( instead of (.
  2. Don't jquery-fy everything, this task is more easy in vanillajs.

function TestCtrl() {
  var tests = document.querySelectorAll('.test');
  
  function hasClassHandTdmm(element) {
    var re = /onHandTdmm\([0-9]+\)/
    var parent = element.parentNode;
    
    if(re.test(parent.className)) {
      parent.classList.add('matches');
    }
    
  }
  
  
  Array.prototype.forEach.call(tests, hasClassHandTdmm);
}

document
  .addEventListener('DOMContentLoaded', TestCtrl);
;
.matches {
  background: pink;
}
<div class="onHandTdmm(1) ">
  <span class="test">TEST</span>
</div>

<div class="">
  <span class="test">TEST 2</span>
</div>
Hitmands
  • 13,491
  • 4
  • 34
  • 69
1

Instead of looping through all the cells and matching the parent that have the class, why not only find rows that have the class, and then manipulate the cell as you need?

For the Regex portion you'll need to use .test, as jQuery selectors don't natively handle regex.

Working JSFiddle: https://jsfiddle.net/j4d3zgqt/4/

var sum = 0;
$("#scLD > tbody > tr").each(function() {
    var $row = $(this);
    var $cell = $(this).find('td:nth-child(2)');

    if (/^onHandTdmm([0-9]+)$/.test($row.attr("class"))) {
        var y = parseFloat($cell.text(), 10) || 0;
        sum += y;
    }
    if ($row.hasClass("onHandGrand")) {
        $cell.html(sum);
        sum = 0;
    }
});
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
0

I believe it may be possible you are overthinking this task. From my understanding, a simple indexOf command should give you the existence of the classname and then you can evaluate the integer that follows separately.

-1

One thing is just try to use val() instead of text() function with the above provided answers.

Danny
  • 45
  • 1
  • 8