Botao said:
I'd like to put a button on a page. When clicking the button, the
table below it gets selected so the user can do Ctrl C to copy the
entire table without using the mouse to select the table which can be
big. How do I do it using javascript?
You'll be able to do the selection by using so-called 'ranges,' which
are so far supported by IE4+ and Mozilla. The copy command can also be
performed, in IE by using execCommand and in Mozilla by using clipboard
helpers (which however require higher security privileges).
I got error "document.MyTable is null or not an object".
That's because the way you try to get a reference to the table object
isn't correct, give a look at
Could anyone help please? Thanks in advance!
<table id="trainOfThought">
<tr><td>As I Am</td></tr>
<tr><td>This Dying Soul</td></tr>
<tr><td>Endless Sacrifice</td></tr>
<tr><td>Honor Thy Father</td></tr>
<tr><td>Vacant</td></tr>
<tr><td>Stream Of Consciousness</td></tr>
<tr><td>In The Name Of God</td></tr>
</table>
<script type="text/javascript">
var Utils = {
NOT_SUPPORTED : {},
DOM : {
getElementWithId : function() {
var func=function(){return Utils.NOT_SUPPORTED;}
if(document.getElementById) {
func=function(id){
return document.getElementById(id);
}
} else if(document.all) {
func=function(id) {
return document.all[id];
}
}
return (this.getElementWithId=func)();
}
},
Ranges : {
create : function() {
var func=function(){return Utils.NOT_SUPPORTED};
if(document.body && document.body.createTextRange) {
func=function(){return document.body.createTextRange();}
} else if(document.createRange) {
func=function(){return document.createRange();}
}
return (this.create=func)();
},
selectNode : function(node, originalRng) {
var func=function(){return Utils.NOT_SUPPORTED;};
var rng=this.create(), method="";
if(rng.moveToElementText) method="moveToElementText";
else if(rng.selectNode) method="selectNode";
if(method)
func=function(node, rng){
rng=rng||Utils.Ranges.create();
rng[method](node);
return rng;
}
return rng=null,(this.selectNode=func)(node, originalRng);
}
},
Selection : {
clear:function() {
var func=function(){return Utils.NOT_SUPPORTED};
if(typeof document.selection!="undefined") {
func=function(){
if(document.selection && document.selection.empty) {
return (Utils.Selection.clear=function(){
if(document.selection)
document.selection.empty();
})();
}
}
} else if(window.getSelection) {
var sel=window.getSelection();
if(sel.removeAllRanges) {
func=function(){
window.getSelection().removeAllRanges();
}
}
sel=null;
}
return (this.clear=func)();
},
add : function(originalRng) {
var func=function(){return Utils.NOT_SUPPORTED};
var rng=Utils.Ranges.create();
if(rng.select) {
func=function(rng){rng.select();}
} else if(window.getSelection) {
var sel=window.getSelection();
if(sel.addRange) {
func=function(rng){window.getSelection().addRange(rng);}
}
sel=null;
}
return (this.add=func)(originalRng);
}
}
};
(function() {
var rng=Utils.Ranges.create();
var ele=Utils.DOM.getElementWithId("trainOfThought");
if(rng!=Utils.NOT_SUPPORTED && ele!=Utils.NOT_SUPPORTED) {
document.write(
"<form>"+
"<input type='button' value='Select!' onclick='"+
"Utils.Selection.clear();"+
"Utils.Selection.add("+
"Utils.Ranges.selectNode("+
"Utils.DOM.getElementWithId(\"trainOfThought\")"+
")"+
")"+
"'>"+
"<\/form>"
);
}
})();
</script>
HTH
Yep.