The jQuery execCommand feature fails to generate a pop-up when used within the contenteditable attribute of an HTML tag

Trying to implement an inline text editor using the jQuery execCommand function. Below is a snippet of my source code:

/*******************************************************************/
/********** Click: inner of contenteditable text-editor div ********/
/*******************************************************************/
$(document).ready(function(){
$(function() {
  $('.text-editor').on("click", function(e) {
/******* Start: Click text to Background Change *********/
$(".text-editor").removeClass("text-click");
$(this).addClass("text-click");
/******* End: Click text to Background Change *********/

/******* START: Click text to tag contenteditable attr. *********/
$(this).attr("contenteditable","true");
/******* End: Click text to tag contenteditable attr. *********/

/******* Start: Click text to Popup class *********/
$(".text-editor").removeClass("popup");
$(this).addClass("popup");
/******* End: Click text to add Popup class *********/

/******* Start: Click text to Popup Div *********/
$(".popup-panel").remove();
var PopupHtml = "<div class='popup-panel show' contenteditable='true'>\
<button type='button' id='bold-text'><i class='fas fa-bold'></i></button>\
<button type='button' id='underline-text'><i class='fas fa-underline'></i></button>\
<button type='button' id='italic-text'><i class='fas fa-italic'></i></button>\
</div>";

$('.text-editor').contents().prop('designMode','off');
$(this).contents().prop('designMode','on');

if(!$('.popup-panel').length){
$(this).append( PopupHtml );
}
/******* End: Click text to add Popup Div *********/
e.stopPropagation()
  }); 

  /*******************************************************************/
  /********** Click: outter of contenteditable text-editor div *******/
  /*******************************************************************/
   $(document).on("click", function(e) {
    if ($(e.target).is(".text-editor") === false) {
/******* Start: Click text to Background Change *********/
$(".text-editor").removeClass("text-click");
/******* End: Click text to Background Change *********/

/******* START: Click text to tag contenteditable attr. *********/
$(".text-editor").removeAttr("contenteditable");
/******* End: Click text to tag contenteditable attr. *********/

  /******* Start: Click text to remove Popup class *********/
  $(".text-editor").removeClass("popup");
    /******* End: Click text to add Popup class *********/

  /******* Start: Click text to Popup Div *********/
  $('.text-editor').contents().prop('designMode','off');
  $(".popup-panel").remove();
    /******* End: Click text to add Popup Div *********/
    }
  });


   $('#bold-text').on("click", function(e) {
   document.execCommand('bold',false,null); 
   });

   $('#underline-text').on("click", function(e) {
   document.execCommand('underline',false,null); 
   });

   $('#italic-text').on("click", function(e) {
   document.execCommand('italic',false,null); 
   });


});
});
.text-editor{ background-color: transparent; border: 1px solid transparent; display: block; }
.text-click{background-color: lightyellow; border: 1px dashed #ccc;}
*[contenteditable="true"] { outline: 0px solid transparent; }

ul.text-option{ margin: 0; padding: 0; }
ul.text-option li{ list-style: none; display: inline-block; }
ul.text-option li a{ padding: 5px; border: 1px solid #ccc; margin-right: 5px; }
button{ margin-right: 5px; }

/* Popup container - can be anything you want */
.popup { position: relative; /*display: inline-block;*/ cursor: pointer; -webkit-user-select: none; -moz-user-select: none;
  -ms-user-select: none; user-select: none;
}

/* The actual popup */
.popup .popup-panel {visibility: hidden;/*width: 160px;*/background-color: #555;color: #fff;text-align: center;border-radius: 6px;
padding: 8px;position: absolute;z-index: 1;bottom: 125%;left: 50%;margin-left: -80px;}

/* Popup arrow */
.popup .popup-panel::after {content: "";position: absolute;top: 100%;left: 50%;margin-left: -5px;border-width: 5px;
border-style: solid;border-color: #555 transparent transparent transparent;}

/* Toggle this class - hide and show the popup */
.popup .show {visibility: visible;-webkit-animation: fadeIn 1s;animation: fadeIn 1s;}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {from {opacity: 0;} to {opacity: 1;}}

@keyframes fadeIn {from {opacity: 0;}to {opacity:1 ;}}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<div class="box">

<h1 class="text-editor" contenteditable="true">First Heading Text</h1>
<h2 class="text-editor">Second Heading Text</h2>
<h3 class="text-editor">Third Heading Text</h3>
<h4 class="text-editor">Forth Heading Text</h4>
<h5 class="text-editor">Fifth Heading Text</h5>
<p class="text-editor">This is paragraph text</p>
</div>

Encountering an issue where selecting text within html contenteditable tags and attempting to format it with the "Bold"/"Underline"/"Italic" buttons from the popup does not yield the desired result. However, when the button set is not within a popup container, like the example below, it works fine:

    <div class="box">
        <h1 class="text-editor" contenteditable="true">First Heading Text</h1>
        <h2 class="text-editor">Second Heading Text</h2>
        <h3 class="text-editor">Third Heading Text</h3>
        <h4 class="text-editor">Forth Heading Text</h4>
        <h5 class="text-editor">Fifth Heading Text</h5>
        <p class="text-editor">This is paragraph text</p>
    </div>

<ul class="text-option">
    <li><button type="button" id="bold-text"><i class="fas fa-bold"></i></button></li>
    <li><button type="button" id="underline-text"><i class="fas fa-underline"></i></button></li>
    <li><button type="button" id="italic-text"><i class="fas fa-italic"></i></button></li>
</ul>

Seeking assistance to rectify the issue. Any help would be greatly appreciated.

Answer â„–1

"Outdated Feature This feature is considered outdated. While it may still be functional in certain browsers, it is recommended to avoid using it as it could be phased out in the future. It is best to find alternative methods."

https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

Attempt to display the return value of the exec command, as it might return false if not supported.

console.log(document.execCommand('underline',false,null))

"Enabling Design Mode in an HTML Document" Could you experiment with enabling the execCommand on the document?

Furthermore, even though it is unlikely, can you execute the execCommand from a jQuery selector, such as:

$(document).execCommand

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining the Immediate ID of a Widget using JQuery

I currently have the following setup: <div id="dualList"></div> I created a plugin but stripped it down for testing purposes. I am encountering difficulties with the plugin not displaying the ID of the div. <script> (function($) { ...

What is the best way to modify the color of a button within an AngularJS function by utilizing the same button?

Currently, I have a function assigned to the ng-click event on a button in order to filter a list. My goal is to change the color of the button once it has been clicked and the filtered list is displayed. I am looking for a way to achieve this within the ...

Eliminating the background color upon clicking

Here is the link structure I have set up: <ul> <li><a href="home.html"><span>home</span></a></li> </ul> However, when I click on the link, a shadow appears. I would like the link to appea ...

Encountering an error stating that $.browser is undefined within a jQuery UI script

Before reaching out for help, I want to clarify that I did not ask this question without attempting to find a solution first. I understand that there are numerous discussions on this topic on various platforms like SO and the internet. However, everyone se ...

Fade-In Effect for CSS Background Image

I'm currently learning CSS and I am trying to create an effect where, on hover over some text, the text disappears and an image fades in. I have been experimenting with different methods but haven't quite achieved the desired outcome. The text i ...

Erasing a comment creates empty spaces

I've encountered an issue with my idea whiteboard where removing comments leaves unwanted blank spaces between ideas. For example, if I have the following comments: But when I delete something: Is there a way to ensure that no gaps are left between ...

Javascript Parameter

Each time I enter scroll(0,10,200,10); into my code, it seems to output the strings "xxpos" or "yypos" instead of the actual values. I attempted to remove the apostrophes around them, but unfortunately, that did not resolve the issue. scroll = function( ...

Error in AngularJS: Failed to retrieve data using $http.post()

One of my current challenges involves: Transferring text data from HTML form text boxes to the Controller using ng-model and ng-submit directives. Sending this data from the controller to a Service. The Issue at Hand: - I am facing difficulties accessi ...

Ways to delete a header from the req object in Express

Can anyone help me understand how to remove a header from the req object in Express? I've heard that using res.disable("Header Name") can do this for the res object, but it doesn't seem to work for req.headers. ...

Automatically adjusting the height of a Bootstrap carousel as the token-input list grows in size

I am working on a carousel that functions as a form. One of the carousel items contains a multi-select box. How can I automatically increase the height of only that specific carousel item as the height of the multi-select box increases? The first image sh ...

How can one effectively restrict the number of tags allowed in a WYSIWYG editor?

It is not enough to limit the tags in a WYSIWYG editor by excluding buttons, as I can still copy page content and paste it into the editor, which it will accept! I am looking for a solution to restrict the allowed tags. For example, I admire how Stack Ove ...

Bootstrap is unable to consistently align elements on a single page

Utilizing bootstrap 4 within my page, I structure it as follows: <div class="container"> <div class="row"> <div class="col-md-12"> <div class="col-md-6"> <label>"GO"</label> ...

A method that sorts an array of objects based on their properties

I am currently working with two specific objects: clinics: [ { id: 1, name: 'New Hampshire Veterinarian Clinic', plans: [ 'handle123', 'handle567', ] }, { ...

Verifying a user's initial visit status using COOKIES in a Servlet

Is there a method to determine if a user is visiting a page for the first time using SERVLET and cookies, without utilizing sessions? I understand how to accomplish this with sessions, but I am specifically looking for a solution that involves only cooki ...

Can you explain the distinction between using call and apply?

Can you explain the distinction between utilizing Function.prototype.apply() and Function.prototype.call() to execute a function? const func = function() { alert("Hello world!"); }; func.apply() compared to func.call() Do performance dispar ...

Uh oh! React encountered a cross-origin error and is unable to access the real error object during development

I've encountered an issue while attempting to extract and display data from the student_learn field in firestore. Despite my efforts to map it to jsx components, I keep receiving a cors-origin error. Below is a screenshot of the collection I'm tr ...

What is the reason behind genNewColor function's malfunction?

I'm having trouble with this code that is supposed to generate a random color. The CSS formatting works perfectly, but I suspect there might be an issue with the function itself. When I try to run the code, it doesn't produce any error messages â ...

Is there a feature in JavaScript that allows for the creation of URLs?

I created an index view displaying cards (like playing cards) in a grid using BootStrap. Each card is within its own div element, and I implemented a jQuery click handler for each div to open a details page when clicked. The redirect from the index to the ...

What are the steps to adjust the width of a website from the standard size to a widescreen

Is it possible to create a "floating" screen adjustment on websites? I know you can set the standard size of pixels, but how do sites adjust for different screen sizes like wider laptop screens? Does it automatically detect the reader's screen size an ...

When the mouse hovers over DIV2, the background image of DIV1 will be replaced

I have a full-screen background div with the ID #background-change. Within that full-screen background, I have 3 Divs named .fullscreen-column-1, .fullscreen-column-2, etc. My goal is to change the background image of #background-change when the mouseove ...