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 5947645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:59:36+00:00 2026-05-22T16:59:36+00:00

I can’t explain the behaviour of the code below. Here’s my entire script <html>

  • 0

I can’t explain the behaviour of the code below. Here’s my entire script

<html>
<head>
<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script type="text/javascript" language="javascript">
var tmpText = '';
$(document).ready(function(){
        tmpText = '';
        $('#btn_bold').click(function(){alert(tmpText);});
        $('textarea').bind('mouseup', function(){
                  tmpText = '';
                  if(window.getSelection){
                    tmpText = window.getSelection();
                  }else if(document.getSelection){
                    tmpText = document.getSelection();
                  }else if(document.selection){
                    tmpText = document.selection.createRange().text;
                  }
                //tmpText = 'hello world';
                alert(tmpText);
        });
});

</script>
</head>
<body>
<button type="button" id="btn_bold">click</button>
<textarea>This is some text</textarea>
</body>
</html>

Try the following operations:

1) Use your mouse to high light text in the text area. You will notice that javascript alerts you the selected text.

2) Press the click button. You will notice javascript will alert you an empty string.

No uncomment tmpText = 'hello world'; and repeat the above steps. This time, you’ll notice both steps 1) and 2) alerts you “hello world”.

How come in the first experiment, step 2) does not alert you the same text as step 1)?

I am testing in google chrome

  • 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-22T16:59:37+00:00Added an answer on May 22, 2026 at 4:59 pm

    Because it doesn’t automatically get converted to string. When you call it straight with alert(), it runs the toString on it, but when you assign to a variable to be later used, it keeps it as selection object and when you try to alert it later on, you presumably won’t have that selection active anymore (because you just clicked the button).

    Add toString() at the end of each of those selections and it should work as intended.

    if(window.getSelection){
                        tmpText = window.getSelection().toString();
                      }else if(document.getSelection){
                        tmpText = document.getSelection().toString();
                      }else if(document.selection){
                        tmpText = document.selection.createRange().text;
                      }
    

    example on jsfiddle

    I recall this being explained quite well in the mozilla developer pages under the getSelection bit, if you want a better explanation why it is like this.

    EDIT: found the link to the page on mozilla, specifically check what they say under “Notes”.

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

Sidebar

Related Questions

Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can someone explain the usage of the dollar sign here.. var updateProgressDiv = $get('updateProgressDiv');
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can i get the source code for a WAMP stack installer somewhere? Any help
Can we put functions to determine the condition for our list comprehensions. Here is
Can we close all known/unknown connections to database with the code? I'm using Access
Does anyone know how can I replace this 2 symbol below from the string
Can someone explain dependency injection with a basic .NET example and provide a few
Can anyone explain why, when I step through my unit tests with the debugger,
Can someone explain what a SpanQuery is, and what are typical use cases for

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.