[webkit-dev] programmatic text selection

Giacomo Luca Maioli luca at tricheco.net
Fri May 26 12:59:06 PDT 2006


I'm sorry I misunderstood your previous request :-D. I must say that  
selecting text is different in every browsers. I posted IE methods  
(whoops! ;-) )

 >> this should work in mozilla firefox :

var selDiv = document.getElementById("selected");		//what I want to  
highlight with selection
var notSelDiv = document.getElementById("notselected");

var range = document.createRange();
range.setStart(selDiv, 0);
range.setEnd(notSelDiv, 0);	

var sel = window.getSelection();	//selection Object
if (sel.rangeCount > 0)
	sel.removeAllRanges();
sel.addRange(range);

--------------------------------

 >> But Safari throws an error while processing the last method (the  
one which actually highlight text), so you should try:

 >> window.getSelection().setBaseAndExtent(selDiv, 0, selDiv, 1);

So you'll only need to play around with this (strange and mostly  
unknown) method.

--------------------------------

You can still use

 >> document.execCommand("Unselect");

to get rid of the selection.

P.S. I made a mistake in my last post. TextRange are only supported  
by Internet Explorer. There is a Range Object in Gecko, anyway.


>> Folks, I've been all over the net without finding a definitive  
>> answer,
>> so I thought I'd come to the source.  Is there any way, in
>> Safari/webkit, to programmatically select some text in javascript?
>> _______________________________________________
>> webkit-dev mailing list
>> webkit-dev at opendarwin.org
>> http://www.opendarwin.org/mailman/listinfo/webkit-dev
>>
>
> Yes, of course. You should use the "document.execCommand(few  
> args)"  command.
> I successfully tested the 'SelectAll", "Copy", "Unselect" arguments  
> in my dashboard widget. It works with text range too.
>
> More info on Msdn pages:
> http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/ 
> execcommand.asp
>
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at opendarwin.org
> http://www.opendarwin.org/mailman/listinfo/webkit-dev
>




More information about the webkit-dev mailing list