Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
spelling in title
Link
Cœur
  • 39k
  • 25
  • 207
  • 282

Modify text in a contenteditable div without resetingresetting the caret (cursor) position

added 31 characters in body; edited title
Source Link
nbrooks
  • 18.2k
  • 5
  • 58
  • 68

jquery regex replace inside Modify text in a contenteditable div without reseting the caret (cursor) position

I am trying to live replace any instances of /any thing in here//any thing in here/ with /any thing in here/<b>/any thing in here/</b> on the fly, as changes are made in a contenteditable div.

My current implementation works, but at every keypress the caret is moved to the beginning of div making the implementation unusable. Is there some way to keep the caret position while replacing the div's contents?

$('.writer').on('keyup', function(e) { $(this).html($(this).html().replace(/\/(.*)\//g, '<b>\/$1\/<\/b>')); }); 

jquery regex replace inside contenteditable div caret position

I am trying to live replace any instances of /any thing in here/ with /any thing in here/ in a contenteditable div.

My current implementation works, but at every keypress the caret is moved to the beginning of div making the implementation unusable. Is there some way to keep the caret position while replacing the div's contents?

$('.writer').on('keyup', function(e) { $(this).html($(this).html().replace(/\/(.*)\//g, '<b>\/$1\/<\/b>')); }); 

Modify text in a contenteditable div without reseting the caret (cursor) position

I am trying to replace any instances of /any thing in here/ with <b>/any thing in here/</b> on the fly, as changes are made in a contenteditable div.

My current implementation works, but at every keypress the caret is moved to the beginning of div making the implementation unusable. Is there some way to keep the caret position while replacing the div's contents?

$('.writer').on('keyup', function(e) { $(this).html($(this).html().replace(/\/(.*)\//g, '<b>\/$1\/<\/b>')); }); 
Source Link
slyv
  • 838
  • 1
  • 10
  • 18

jquery regex replace inside contenteditable div caret position

I am trying to live replace any instances of /any thing in here/ with /any thing in here/ in a contenteditable div.

My current implementation works, but at every keypress the caret is moved to the beginning of div making the implementation unusable. Is there some way to keep the caret position while replacing the div's contents?

$('.writer').on('keyup', function(e) { $(this).html($(this).html().replace(/\/(.*)\//g, '<b>\/$1\/<\/b>')); });