...

View Full Version : Select everything inside a div tag



tobart
07-25-2002, 04:08 PM
I am trying to select everything inside a <div>hello</div> tag but I cant get it to work. Is it impossible or not?
The code looks like this:

<script language="Javascript">
<!--

function HighlightAll() {
var tempval=eval("document.all.testar")
tempval.focus()
tempval.select()
}
//-->
</script>
</head>
<body>
<div id="testar">hello</div>

Please tell me what i've been doing wrong.......

Tobart

ShriekForth
07-25-2002, 05:27 PM
I think select() is a method on form objects, like a text input or text area. I have used it on that, but never on a div. After some research, it seems you can use it on a TextRange object, but I'm not sure if that would help you or not.

This might help with how the TextRange works...

http://www.webreference.com/js/column12/textrange.html

ShriekForth

tobart
07-25-2002, 05:34 PM
Thank you I will check it out

jkd
07-25-2002, 08:27 PM
var range = document.createRange();
range.selectNode(document.getElementById('testar'));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);


That is how you would do it in NS6+/any Gecko browser.

tobart
07-26-2002, 02:09 PM
Can you do it for Ie too?
I will use it for ie only..

jkd
07-26-2002, 07:39 PM
After digging through the documentation at MSDN, this seems to work:

var textrange = document.body.createTextRange();
textrange.moveToElementText(document.getElementById('testar'));
textrange.select();

Why are you going to do this IE-only though when you have crossbrowser code? Unless this is for an intranet, it is always a good thing to be crossbrowser...

tobart
07-28-2002, 11:14 AM
Thank you very much and yes it is for the Intranet...
We only have IE and it's not my choice but i have to respect the boss requests.....

tobart
08-07-2002, 01:20 PM
<!--
function HighlightAll() {
var textrange = document.body.createTextRange();
textrange.moveToElementText(document.getElementById('show'));
textrange.select();

}
//-->



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum