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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.