5Jul/090
jQuery Select Table Column or Row
Table rows are pretty easy to work as all the td elements are contained within one tr, but how would you grab all the tds in a single column? Why, by using these selectors of course!
$.fn.row = function(i) {
return $('tr:nth-child('+(i+1)+') td', this);
}
$.fn.column = function(i) {
return $('tr td:nth-child('+(i+1)+')', this);
}
// example - this will make the first column of every table red
$(document).ready(function() {
$('table').column(0).css('background-color','red');
});
return $('tr:nth-child('+(i+1)+') td', this);
}
$.fn.column = function(i) {
return $('tr td:nth-child('+(i+1)+')', this);
}
// example - this will make the first column of every table red
$(document).ready(function() {
$('table').column(0).css('background-color','red');
});
I like my selectors zero-based, but you can get rid of the +1 if you don't like it.
10Jun/090
Human-readable file size in JavaScript
"size" is in bytes, the rest you should be able to figure out.
function readableFileSize(size) {
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = 0;
while(size >= 1024) {
size /= 1024;
++i;
}
return size.toFixed(1) + ' ' + units[i];
}
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = 0;
while(size >= 1024) {
size /= 1024;
++i;
}
return size.toFixed(1) + ' ' + units[i];
}
8May/093
jQuery Select Text Range
Here's a jQuery function I wrote which you can use to select a range of text in an input field.
$.fn.selectRange = function(start, end) {
return this.each(function() {
if(this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if(this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
return this.each(function() {
if(this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if(this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};