Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How to toggle the case of individual words in sentences displayed in HTML form input boxes by double clicking them?

Double clicking a word in a sentence in an HTML form input box will highlight that word. I want each double click to toggle the case of the highlighted word between lower and upper case.

    <input id="vnib" type="text" value="#ATitle#">

Example: the title showing in the input box is "The longest Day". When I double click on the word "longest" it will change to "Longest".

Why this? I need to correct hundreds of capitalization errors in a list of 30,000 titles and want to save hundreds of key presses. Right now I have to select the initial letters of each incorrectly capitalized word and then type in the letters with the correct case.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a simple HTML + pure JS solution.

let content = document.getElementById("content");
content.addEventListener("dblclick", changeSelectionCase);
function changeSelectionCase(e) {
  let selection = window.getSelection();
  if (selection && selection.rangeCount > 0) {
    let selectionRange = selection.getRangeAt(0);
    let startOffset = selectionRange.startOffset
    content.textContent = content.textContent.substring(0, startOffset) +
                          content.textContent[startOffset].toUpperCase() +
                          content.textContent.substring(startOffset + 1);
  }
}
<p id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora nostrum optio suscipit cumque adipisci molestias inventore officia ea corrupti dolore alias nemo iure, beatae porro soluta quo aliquam ut facere.</p>

@Commata, per your comments, made it easier. Just save the below code as an HTML file and open it up with your browser. It contains an editable paragraph, so you can copy/paste your text on it. Then double click on the words you want to toggle the case of the first letter.

<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        p:before {
            content: "(Paste content here)";
            font-weight: bold;
            margin-right: 1rem;
        }
        p {
            margin: 2rem;
            padding: 2rem;
        }
    </style>
</head>

<body>
    <p contenteditable="true" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora nostrum
        optio suscipit cumque adipisci molestias inventore officia ea corrupti dolore alias nemo iure, beatae porro
        soluta quo aliquam ut facere.</p>
</body>
<script>
    let content = document.getElementById("content");
    content.addEventListener("dblclick", changeSelectionCase);
    function changeSelectionCase(e) {
        let selection = window.getSelection();
        if (selection && selection.rangeCount > 0) {
            let selectionRange = selection.getRangeAt(0);
            let startOffset = selectionRange.startOffset;
            let upperCase = content.textContent[startOffset].toUpperCase();
            let toggledCase = upperCase === content.textContent[startOffset]
                ? content.textContent[startOffset].toLowerCase()
                : content.textContent[startOffset].toUpperCase();
            content.textContent = content.textContent.substring(0, startOffset) +
                toggledCase +
                content.textContent.substring(startOffset + 1);
        }
    }
</script>

</html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!