...

View Full Version : document.body.createTextRange(), selecting...



whammy
02-20-2003, 10:25 PM
Hello,

A colleague and I are trying to find a way to automatically select ONLY a specific range of text within a page, using

document.body.createTextRange()

and somehow referencing the id of a span... however NOTHING we have tried works. We got it to select the first thing sometimes (they may often contain the same text), and at other times it would select all spans with identical text - but we're finding it impossible to isolate the span by id and select the text just by clicking on the span. It's pretty perplexing, and this is not the type of javascript we usually mess around with... any help?

The below obviously doesn't work, just a general example of what we're trying to do... :mad:



<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function textSelect(lblnumber)
{
alert(lblnumber);
var range = document.???.innerText;
range.createRange();
range.select();
}
// -->
</script>
</head>
<body>
<form id="form1" action="" method="post">
<span id="lblFasdffname" onclick="textSelect(this.id)"><*fname*></span>
<span id="lblFasd234name" onclick="textSelect(this.id)"><*fname*></span>
<span id="lblFasdasd3name" onclick="textSelect(this.id)"><*fname*></span>
</form>
</body>
</html>

jkd
02-20-2003, 11:37 PM
*sigh*

var range = document.body.createTextRange();
range.moveToElementText(document.all['theID']);
range.select();


ugghhhh, look what you made me do. icky! :(

whammy
02-20-2003, 11:59 PM
Hey, I try to avoid this kind of icky stuff, myself, and the "Bible" didn't help much when you're trying to figure it out quick...

But that might help someone I'm working with, so thanks! :)

I'll let you know how it goes in the morning... but I suggested we post in this forum for obvious reasons. :)

kwhubby
02-21-2003, 03:25 AM
icky??????
how is that icky??
whats wrong with doing it that way?

jkd
02-21-2003, 03:43 AM
Because IE-only stuff is generally icky. Especially TextRanges. There is a wonderful thing called DOM2 Range, and IE has do to their own thing with TextRange. Brings a bad taste to my mouth.

The DOM2 Range way of creating a range around some text using the same information:

var range = document.createRange();
range.selectNode(document.getElementById('theID'));

Then you could surroundContents() with a styled span or something to indicate selection. (Selection is part of the browser, and not the webpage, therefore DOM doesn't deal with actual selections).

The Gecko way of selecting text:

var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(aRangeInstanceLikeTheOneWeJustCreated);

Is only a little less icky because it provides an intelligent interface to user-selections, and uses DOM2 Range instances to describe them.

whammy
02-21-2003, 02:08 PM
Thanks, it works. We're also going to try to do some browser-checking so we can use the gecko method as well... :)

whammy
02-21-2003, 09:31 PM
That works just great too. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum