Jump to content

Insert Tags at Caret


Cetanu

Recommended Posts

So, I am working on this very small editing feature that I would like to behave like forums' BBCode insert things. I have searched high and low for a tutorial or something of the sort to detect where a caret is and then insert my code there....

...I really want it to be slim, and it should work with a textarea. Any help?

Thanks!!!

Link to comment
Share on other sites

of course, this sort of assumes there's gonna be only 1 caret in the textarea... if not, you're going to be having something insert at every caret found... but you weren't very specific in your request, now were you.

<textarea id='blah'>some ^ example</textarea>
<script type='text/javascript'>
var something = "random";
document.getElementById('blah').value = String(document.getElementById('blah').value).split('^').join(something);
</script>

 

Link to comment
Share on other sites

hmm well after some research, there's no "thin" way to do this.  I would suggest you start by looking at the js for <insert forum of your choice here>.  For instance, here is the js file for this board:

 

http://www.phpfreaks.com/forums/Themes/default/scripts/script.js?rc2

 

starting around line 338 function storeCaret(..)

 

 

Link to comment
Share on other sites

hmm well after some research, there's no "thin" way to do this.  I would suggest you start by looking at the js for <insert forum of your choice here>.  For instance, here is the js file for this board:

 

http://www.phpfreaks.com/forums/Themes/default/scripts/script.js?rc2

 

starting around line 338 function storeCaret(..)

 

Cool! That's helpful, thanks.  :D

Link to comment
Share on other sites

Yes there is a way to easily do this

 

go to google and type in MDC Selection

 

anchorNode

anchorOffset

focusNode

focusOffset

 

You can use these 4 properties of the window.selection (even if there is no selection) and work it out that way.  I don't remember exactly but i did this before and it worked.

 

Maybe also take a look at MDC Range

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.