I have a page with a table of database records. In one of the table cells, I have an onclick event in the <td> tag that calls a function.
<td onclick="javascript:GetText(this)">
What I want to happen is that when the user clicks on the cell, the text within the cell becomes selected. I have tried and tried several different pieces of code and I have searched the web for help, but nothing that I do seems to work. Copied below is my function. Can anybody tell me what is wrong with my code and how to fix it?
This is what I came up with using my JavaScript reference book. It is for Firefox 3.0.
function GetText(TableRowObj)
{
// These 3 lines work so I must be getting the
// element object reference.
//var SelText = Trim(TableRowObj.innerHTML);
//window.alert("Cell Text: "+SelText);
//window.alert("Length: "+SelText.length);
// These 2 lines were something that I saw
// on one of the posts on the web
// that I found, but it doesn't work.
//TableRowObj.focus();
//TableRowObj.select();
var SelRange = document.createRange();
SelRange.selectNodeContents(TableRowObj);
window.selection.addRange(SelRange);
}
For Internet Explorer (version 6.0), I replaced the last 3 lines of the above function with the following, and it does not work either.
var SelRange = document.body.createTextRange();
SelRange.moveToElementText(TableRowObj);
SelRange.select();
//window.alert("Type: "+document.selection.type);
I appreciate any help.
<td onclick="javascript:GetText(this)">
What I want to happen is that when the user clicks on the cell, the text within the cell becomes selected. I have tried and tried several different pieces of code and I have searched the web for help, but nothing that I do seems to work. Copied below is my function. Can anybody tell me what is wrong with my code and how to fix it?
This is what I came up with using my JavaScript reference book. It is for Firefox 3.0.
function GetText(TableRowObj)
{
// These 3 lines work so I must be getting the
// element object reference.
//var SelText = Trim(TableRowObj.innerHTML);
//window.alert("Cell Text: "+SelText);
//window.alert("Length: "+SelText.length);
// These 2 lines were something that I saw
// on one of the posts on the web
// that I found, but it doesn't work.
//TableRowObj.focus();
//TableRowObj.select();
var SelRange = document.createRange();
SelRange.selectNodeContents(TableRowObj);
window.selection.addRange(SelRange);
}
For Internet Explorer (version 6.0), I replaced the last 3 lines of the above function with the following, and it does not work either.
var SelRange = document.body.createTextRange();
SelRange.moveToElementText(TableRowObj);
SelRange.select();
//window.alert("Type: "+document.selection.type);
I appreciate any help.