<html>
<head>
<style type="text/css">
.sel {background-color: #99ff33; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(function () {
function col(cell) {
return cell.parent().children("td").index(cell);
}
var start = null;
function selectTo(cell) {
if (start == null)
return;
$("td").removeClass("sel");
var stop = $(cell);
var tbl = start.closest("table");
var rs = tbl.children("tbody").children("tr");
var r0 = rs.index(start.parent()), c0 = col(start);
var r1 = rs.index(stop.parent()), c1 = col(stop);
var sum = 0;
for (var i = r0; i <= r1; i++) {
var cells = $(rs.get(i)).children("td");
for (var j = c0; j <= c1; j++) {
var cell = $(cells.get(j));
cell.addClass("sel");
sum += Number(cell.html());
}
}
$("#total").html(""+sum);
}
$("table").mousedown(function () {
return false;
});
$("td").mousedown(function () {
start = $(this);
selectTo(this);
return false;
});
$("td").mouseover(function () {
selectTo(this);
});
$("td").mouseup(function () {
selectTo(this);
start = null;
});
$("body").mouseup(function () {
start = null;
});
});
</script>
<body>
<table>
<tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr>
<tr> <td>2</td> <td>4</td> <td>6</td> <td>8</td> </tr>
<tr> <td>3</td> <td>6</td> <td>9</td> <td>12</td> </tr>
</table>
<p>Total of selected elements: <span id="total"></span></p>
</body>
</html>
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |