is there any way we can manually change the text selection in mobile html in javascript and while changing the selection side arrows should remain there.
for example, if a user selects one word in div and we can select the whole text of div after that through javascript

Deepak RawatRank #646 · 3.7K reputationPosted Jul 4, 2023, 7:34 AM EDT
In JavaScript, you can programmatically manipulate text selection in a mobile HTML environment using the Selection API. While it's not possible to directly control the native selection handles (side arrows) on mobile devices, you can achieve the desired effect by extending the selection programmatically. Here's an example of how you can select the whole text within a after the user has made a word selection:
JavaScript
Know the answer? Forums 01
Arun PottiAnswers in the forums20answers02
Tahir AnsariAnswers in the forums5answers03
Kamlesh KumarAnswers in the forums5answers04
Sam HobbsAnswers in the forums4answers05
Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.Join Full leaderboard → 2026
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
function selectWholeText() { const divElement = document.getElementById('your-div-id'); // Create a range that encompasses the entire text within the div const range = document.createRange(); range.selectNodeContents(divElement); // Get the selection object const selection = window.getSelection(); // Remove any existing selections selection.removeAllRanges(); // Add the new range to the selection selection.addRange(range); }In the above code snippet, replace element you want to select. You can call the
'your-div-id'with the actual ID of theselectWholeText()function when triggered by an event, such as a button click or another user action.By programmatically creating a range and adding it to the selection, you can extend the selection to cover the entire text within the. However, keep in mind that you won't be able to control the appearance or position of the native selection handles (side arrows) on mobile devices. The appearance and behavior of these handles are managed by the device's operating system and browser.
It's worth noting that the Selection API has some variations and limitations across different mobile browsers. Therefore, it's recommended to test the behavior in your target browsers and consider fallback options or alternative approaches if needed.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts