Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select textnode inside table cell 1

Status
Not open for further replies.

MikeAJ

Programmer
May 22, 2002
108
US
I'm trying to highlight/select the text inside a table cell when the cell is clicked, but having problems. Here's what I thought would have worked.

Code:
<td onclick="this.childNodes[0].select()">

Anyone know how this can be accomplished?

Thanks!
 
actually, i lied. referencing this site i came up with the following:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--

function doit(o) {
    if (document.body.createTextRange) {
        var tr = document.body.createTextRange();
        tr.moveToElementText(o);
        tr.select();
    } else if (document.createRange) {
        var tr = document.createRange();
        tr.selectNode(o);
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(tr);
    }
}

//--></script>
</head>

<body>

<table border="1"><tr>
    <td onclick="doit(this)">hi</td>
    <td onclick="doit(this)">hello</td>
</tr></table>

</body>
</html>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
If it's selecting text so that you can copy it you're after, you could style an input to have no borders, but this will only work in some browsers.

Alternatively, if it's purely the effect of white-on-blue (or whatever colours) you're after, then you could use CSS to do this.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks guys! cLFlaVA, your example was perfect. Exactly what I was after.

Thanks Again!
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top