Jump to content

Text Selection help (bbcode function)


micmania1

Recommended Posts

Hi, I'm trying to create a bbcode function with functionality similar to that of which is used on this site.

 

Here's my function:

function bbCode(code) { 
var text = "";
var e = 'message'; // ID

if (document.all) {
	text = document.selection.createRange().text; 
} else {
	text = document.getSelection();
}

if (text != '') { // text must be highlighted
	// Set new highlighted value
	if (document.all) { 
		document.selection.createRange().text = '[' + code + ']' + text + '[/' + code + ']';
	} else {
		var setVal = document.getSelection() = '[' + code + ']' + text + '[/' + code + ']';
	}
} else {
	document.getElementById(e).innerHTML = document.getElementById(e).innerHTML + '[' + code + '][/' + code + ']';
}
}

 

The problem i'm having is detecting where the selected text is from. For example I could highlight the footer of my website which has no relation to the bbcode function, but when the 'bold' tag is clicked, it will wrap the footer in bold tags or any text on that page which I select.

 

Could somebody tell me how I can detect where the selected text is?

 

Thanks for your time.

Michael

Link to comment
https://forums.phpfreaks.com/topic/126927-text-selection-help-bbcode-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.