Another option for document.execCommand('selectAll',false,null)

I've come across a piece of code that allows me to select the text in a div whenever a user clicks on it:

<table>
  <tr>
    <td>
      <div contenteditable onClick="document.execCommand('selectAll',false,null)">I'm editable</div>
    </td>
  </tr>
  <tr>
    <td>
      <div contenteditable>I'm also editable</div>
    </td>
  </tr>
  <tr>
    <td>I'm not editable</td>
  </tr>
</table>

Unfortunately, I have discovered that document.execCommand has been deprecated according to this source. I am now looking for a suitable alternative. How can I make this change?

Answer №1

After reading through Alvaro Tihanyi's comment and Shilly's comment, I came across this solution:

function selectText(element) {
    if (document.selection) { // For IE
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(element);
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
    }
}
<table>
  <tr>
    <td>
      <div id="selectable" contenteditable onclick="selectText(this)">I'm editable</div>
    </td>
  </tr>
  <tr>
    <td>
      <div contenteditable>I'm also editable</div>
    </td>
  </tr>
  <tr>
    <td>I'm not editable</td>
  </tr>
</table>

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

Refresh a TextBox using an Ajax Response

Is there a way to dynamically update a textbox with the response from an ajax call? I've managed to get the response and assign it to the textbox using: document.getElementById("testPad").value = xmlHttpRequest.responseText; The issue is that the en ...

Using optional chaining and logical assignment in JavaScript

I am facing an issue while trying to determine if a property exists in my object or not. If it exists, I want to increase a number associated with it; if it doesn't, I want to create the property and set the value to 1. I have tried using optional cha ...

Tips for positioning a button next to a text area

I am facing an issue with aligning my text area and button vertically on the same line. Despite my attempts, the button keeps getting pushed downwards. Can someone please provide me with a solution to align them properly? Thank you. Here is the HTML code ...

Differences in file loading in Node.js: comparing the use of .load versus command-line

Currently, I am in the process of developing a basic server using vanilla JavaScript and Node.js. For this purpose, I have created a file named database.js, which includes abstractions for database interactions (specifically with redis). One of my objecti ...

HTML table's horizontal width does not match the header's size

I'm having an issue with my HTML table. The rows are not aligning properly with the header width. Can anyone help with fixing this? I'm not very familiar with CSS, so any guidance would be greatly appreciated. Here is an example of the HTML sour ...

I am unable to comprehend the function definition

I have familiarity with different types of JavaScript function declarations such as expression functions and anonymous functions. However, I am unsure about the syntax used in these two specific functions: "manipulateData: function (input)" and "getDataByI ...

Changing the event handler of a jQueryUI accordion dynamically from "click" to "mouseover" as needed

I am facing a scenario where I need to drag an item from a list and drop it into a target within an accordion. The challenge is that the target may be in a panel that is not currently open. To address this issue, I am looking to dynamically switch the acc ...

I'm currently trying to scrape websites that have JavaScript enabled, but I'm having trouble extracting any content from them

from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import ...

Understanding Angular's transclusion functionality and how it interacts with scopes

I've been working on creating a directive for click-to-edit input fields. My goal was to make it an attribute type directive that can work with various types of input fields by simply transcluding the input field itself. However, I've encountere ...

JQuery Form Wizard - Adding a Finish Button Linked to a Page

Having some difficulty linking the finish button on my Jquery Form Wizard within my Flask App to a link. I attempted to test the functionality by creating a simple alert, but that didn't work either. I'm certain that I missed a step somewhere, b ...

Can you explain the concept of an environment variable in the context of Node/Express?

This question may seem basic, but I haven't found a clear explanation for it yet. In my experience with Node/Express, I always set the following variable: var port = PROCESS.env.PORT || 9000 I understand that PROCESS.env.PORT is related to environme ...

Ways to combine all similar key values into an array

Apologies if the question seems unclear. Essentially, I am dealing with an object structured as follows: [{"6":6.5},{"4":4.2},{"6":6.3}] My goal is to eliminate duplicate keys while preserving their values, and merge them into a single unique key, formin ...

What is the best way to prevent table cell borders from encroaching on the padding area of the cell?

When dealing with table cell elements that have borders, it's common for them to not respect the vertical height of the cell containing them. Sometimes a child element's borders may overlap the padding or border of its containing cell. To prevent ...

The Bootstrap button is having trouble rendering correctly

I attempted to incorporate a Bootstrap search box in my code, expecting it to display like this: https://i.sstatic.net/bPNsE.jpg However, for some reason, it is not rendering correctly within my layout, which resembles this: https://i.sstatic.net/x3wyk.jp ...

Coat the div with a uniform shade of pure white

I need assistance on applying a solid white background color to hide the text behind it. For better understanding, I have attached a screenshot as a reference. https://i.stack.imgur.com/f8Qd9.png The issue arises when I click on the dropdown in the heade ...

Strategies for filtering an array of objects using another array of items

Is there a way to convert the array of objects (packageMap) into a string of names based on an array of matching ids? I think it might involve a combination of filter and map, but I'm having trouble figuring it out! const packageMap = [ { id: ...

Share your hefty files through an online portal

What is the most effective method for allowing users to upload large files from their web browsers to a server? We are talking about file sizes of 200MB or even up to several gigabytes. I have brainstormed some potential solutions to this issue, although I ...

Applying CSS to align the first line to the left and right, while ensuring that wrapped lines are aligned to the

Struggling with CSS to align drink names and descriptions on a bar's menu. Want the name left aligned and the description right aligned, even if wrapped lines are also right aligned. Current progress can be seen at http://jsfiddle.net/H96XQ/ CSS: . ...

Eliminate repeated entries in a drop-down menu and display them with commas in between

I am working with a list that contains various language combinations: German to English German to Spanish German to Chinese German to French English to Spanish English to French English to Greek English to Portuguese Does anyone have suggestions on how ...

Remove the PHP MySql record of the currently logged in user

I'm currently working on addressing an issue I've encountered. My goal is to remove a logged-in user from a MySQL database once they click a specific button. The code involved is named changepw.php <?php $servername = "localhost"; $username ...