Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jquery:  select on highlight like excel AutoSum

Posted on 2013-06-03
2
539 Views
Last Modified: 2013-09-07
We are using a table structure and want to Auto SUM the cells that are highlighted.

Similar to this

<table>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>

If the user highlights columns 1 and 2 a DIV will do the calculation and return 7.  If the user selects the entire row it will return 12 in the corner DIV.

Any thoughts on how to do this?

Thanks in advance...!

sk
0
Comment
Question by:skillilea
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39217540
Hi, see the working example here: http://jsfiddle.net/robert_schutt/EyD3J/ 

You can click on the individual cells or the row/column headers and selection will toggle, total will be calculated with each change.

The generated jsFiddle code for reference:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo by robert_schutt</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-git.js'></script>
<style type='text/css'>
.sel {
    background-color: red;
}
.tot {
    float: right;
    border: 2px solid green;
}
</style>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$('td').on('click', function(){
    $(this).toggleClass('sel');
    calcTot();
});
$('tr:first th').on('click', function(){
    $('tr:not(:first) td:nth-child('+($(this).index()+1)+')').toggleClass('sel');
    calcTot();
});
$('tr:not(:first) th').on('click', function(){
    $(this).closest('tr').find('td').toggleClass('sel');
    calcTot();
});

function calcTot() {
    var tot = 0;
    $('td.sel').each(function(){
        tot += 1 * $(this).text();
    });
    $('div.tot').html(tot);
}
});//]]>  
</script>
</head>
<body>
<table border="1">
<tr>
<th></th>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
</tr>
<tr>
<th>row 1</th>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<th>row 2</th>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</table>
<div class="tot">
</div>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:skillilea
ID: 39473224
tnx tons!

sk
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question