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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.selection.createRange()

Status
Not open for further replies.

nickdel

Programmer
May 11, 2006
367
GB
Hey, this could be a tricky one to explain but I'll give it a go...

What I have is a textarea and I'm putting together what is almost like wysiwyg style editing options. So for example the user highlights some words in the textarea and selects the "bold" option and I use this to apply the html tags:

Code:
TextSelection= document.selection.createRange().text;
	if(TextSelection!=''){
	document.selection.createRange().text = "<b>" + TextSelection+ "</b>";
	TextSelection.clear();

This works just nicely so i thought I would expand and give the users the options to add hyperlinks but obviously I would have to get the URL off them, so once they highlight the required text I have a div pop up to ask them for the URL however as soon as they try to enter this then obviously my original text selection loses focus.

Soooo... my question is, once the user enters the URL in the popup how can I then amend the originally selected text? (does document.selection.createRange() store text location and length?)

Many thanks

Nick
 
What ya know, this turned out to be really simple!

Here's what i done for anyone else in a boogle...

Code:
var RememberSelection
	
	function GetLink(){

	theSelection = document.selection.createRange().text;
	if(theSelection !=''){
	RememberSelection = document.selection.createRange()
		document.getElementById('LinkURL').style.visibility = 'visible';
		}
	}
	
	function AddLink(){
	LinkToURL = document.getElementById('URLLink').value
	
	if(LinkToURL != ''){
		RememberSelection.text = '<a href="' + LinkToURL + '" target=_new>' + theSelection + '</a>';
	}
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top