Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:37:41+00:00 2026-05-31T17:37:41+00:00

I see that when I select text in a text-box with the help of

  • 0

I see that when I select text in a text-box with the help of ‘onfocus’ function, I don’t get the behaviour I expect.

Here is the demo URL: http://jsfiddle.net/BquZz/

A copy of the code follows:

<!DOCTYPE html>
<html>
<head>
<title>Select all</title>
<script type="text/javascript">
var text;
var log;

function select()
{
    text.select();
    log.innerHTML += ' - select';
}

window.onload = function() {
    text = document.getElementById('text');
    log = document.getElementById('log');
    log.innerHTML = 'start';

    text.onfocus = select;
}
</script>
</head>
<body>
<input type="text" id="text" readonly="readonly" value="hello, world">
<div id="log">
</div>
</body>
</html>

I repeat the following experiment multiple times.

  1. Click somewhere outside the text-box in order to blur the text-box.
  2. Click on the text-box in order to focus the text-box.

I expect that every time at the end of step 2, the string “hello, world” in the text-box should be selected. However, this is not what I observe. With Iceweasel 11.0 on Debian GNU/Linux (Wheezy), at the end of alternate experiments “hello, world” is not selected. In the remaining experiments, sometimes I see the string “hello, world” completely selected and sometimes, partially selected. With Chrome 18.0.1025.33 beta on Debian GNU/Linux (Wheezy), I rarely get the desired result. Most of the times, nothing is selected.

I know a way to fix this. Change text.onfocus = select to text.onclick = select. With the select() function invoked with onclick, I get the expected result. Here is a demo URL which shows the desired result using ‘onclick’: http://jsfiddle.net/5EZwR/

Could you please help me to understand why I do not get the expected result with ‘onfocus’ but I get it with ‘onclick’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:37:42+00:00Added an answer on May 31, 2026 at 5:37 pm

    I think the problem is that .select() does two things: it sets the selection range to include all the text, and it causes the field to be focused. The browsers appear to dislike the repeated selection, though Chrome and Firefox behave differently.

    If you change it to:

    text.setSelectionRange(0, 10000);
    

    then it works in Firefox. (I think it’d have to be different for Internet Explorer.)

    edit — This Stackoverflow question has a good answer (Mr. Dimitrov’s) that should work for all browsers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text box in wxpython (see below) that stores the name of
Using IE8's developer mode, I see that select lists are implemented using partial postbacks.
When I attempt to do something like SELECT Max(ObjectId) FROM Objects; I see that
I'm trying to position the jquery validate message under the select box and text
This works, does anyone see anything I shouldn't be doing? My function that is
I see various ways to update a input field from a select box but
I am trying to select the text from a text box and pass it
I see that within MySQL there are Cast() and Convert() functions to create integers
I see that in PL/SQL it is possible to set the session state of
I see that EF can update a model based on an existing database schema.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.