jquery each function to get the class value content

12,580

Solution 1

This should do it:

var sum = 0;
$('#activities').nextAll('.rows').find('.value').each(function() {
    var n = parseInt($(this).text(), 10); 
    if (!isNaN(n)) sum += n; 
});

Solution 2

you can select .rows elements directly instead of traversing the DOM by using multiple next() methods, try the following:

var sum = 0;  
$(".rows .value").each(function(){
       var val = parseInt($(this).text(), 10)
       if (!isNaN(val)) {
            sum += val
       }          
})   

DEMO

Solution 3

Please check here for the solution.

$('#GetTitles').click(function(){
    $('div.rows .title').each(function(){
        alert($(this).text());
    });
});

JSFiddle Sample Solution

Solution 4

To get each individual value in an array:

var values = $(".value", ".rows").map(function(i, e) {
    return parseInt(e.innerHTML, 10);
})​;

FIDDLE

To get the combined sum of the values

var value = 0;
$.each($(".value", ".rows"), function(i,e) {
    value += parseInt(e.innerHTML, 10);
});

FIDDLE

Share:
12,580
Seetha Raman
Author by

Seetha Raman

Updated on June 27, 2022

Comments

  • Seetha Raman
    Seetha Raman about 2 years

    script

    $("#activities").next().next().each(function(i) { });
    

    html

    <div class="head" id="activities" >Activities</div>
    <div class="sub-head"> <span class="title">Activity</span> <span class="value">Min</span> <span class="unit">Calories</span> </div>
    <div class="rows"> <span class="title">bicycling</span> <span class="value">30</span> <span class="unit">174</span> </div>
    <div class="rows"> <span class="title">fishing and hunting</span> <span class="value">30</span> <span class="unit">261</span> </div>
    <div class="rows"> <span class="title">transportation</span> <span class="value">30</span> <span class="unit">261</span> </div>
    <div id="activitiesData"></div>
    

    can anybody please help me to get datas from this div using each in jquery?