user2340414
user2340414

Reputation: 89

Getting values from cells in table using class. Jquery

How can I get values from cells in table using class? Col2, Col3, Col4 have common class "punkty". In Col5 I want set result of addition from Col2,Col3,Col4. In my recent solve, I'm getting string values from row, example "13230", by I would getting 13 and 23 and 0, then input value 36 to Col5.

$("#tab tr").each(function (idx) {
  var ar = $(this).find('.punkty').text();
  alert(ar);
}

JSFiddle

Anybody can help me? :)

Upvotes: 0

Views: 735

Answers (1)

PSL
PSL

Reputation: 123739

Try this:-

Fiddle

You can use the text attributes function to calculate the sum from siblings and set its own text.

 $('#tab tr .suma').text(function(){
   var sum = 0; 
    $(this).siblings('.punkty').each(function(){ //Iterate through the siblings to calculate the sum.
     sum += +$(this).text();
    })
 return (sum==0 ? '-' : sum) ; //return '-' if no sum.
});

Upvotes: 1

Related Questions