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
https://forums.phpfreaks.com/topic/184576-insert-tags-at-caret/
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
https://forums.phpfreaks.com/topic/184576-insert-tags-at-caret/#findComment-974422
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
https://forums.phpfreaks.com/topic/184576-insert-tags-at-caret/#findComment-974478
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
https://forums.phpfreaks.com/topic/184576-insert-tags-at-caret/#findComment-974481
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
https://forums.phpfreaks.com/topic/184576-insert-tags-at-caret/#findComment-974548
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.