Select text again after execCommand execution

171
May 23, 2017, at 06:25 AM

I'm building a text editor using jQuery.

I have a content-editable div and when I click the <span id="bold-text"> I can change the style of selected text:

$('span#bold-text').mousedown(function(e){
    document.execCommand('bold', false, null);
});

After execution of document.execCommand (or maybe because clicking) span selection loose focus.

How can I auto select same text after execCommand?

Answer 1

You might need to store the last focused item within jQuery (exclude the span):

var toFocus;
$("*:not(span#bold-text)").blur(function() {
  toFocus = this;
});

Then re-focus it:

$('span#bold-text').click(function(e){        
  document.execCommand('bold', false, null);    
  if(toFocus){
    $(toFocus).focus(); 
  }        
});
READ ALSO
Include imagesloaded in infiniteajaxscroll plugin

Include imagesloaded in infiniteajaxscroll plugin

I have several masonry isotope grids on my pageNow I implemented successfully the jquery-ias pagination plugin for some of them

171
How to disable changing GoJS diagram after page refreshing?

How to disable changing GoJS diagram after page refreshing?

My question will be simpleI'm new in GoJS diagrams building

185
Is it possible to pull a React component prop data using jQuery?

Is it possible to pull a React component prop data using jQuery?

I am building a Chrome Extension that pulls various data points from the DOMThe website that I am pulling data from was built with React and I was curious if there was a way to get prop data from the hidden React DOM using javascript running from within...

145
How do I get my red div to change background color to toggle between red and green when I click on the swap button?

How do I get my red div to change background color to toggle between red and green when I click on the swap button?

In the following code, how do I get my red div to change background color to toggle between red and green when I click on the swap button?

152