Results 1 to 2 of 2

Thread: FireFox 2/3 setSelectionRange() Problem

  1. #1
    Join Date
    Jul 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default FireFox 2/3 setSelectionRange() Problem

    Hello all,

    I'm running into unexpected behavior with the setSelectionRange() method. I have Searched The Fabulous Web for a while but I have not been able to come up with anything helpful. I have had others reproduce this issue in their installations of FireFox.

    I have a textarea with some text that I would like to select for the user and, for FireFox, I am using the setSelectionRange() method to accomplish this. This seems to work fine, until the user clicks inside of the textarea. Then the setSelectionRange() method seems to fail, without producing an error.

    I have provided a short snippet of HTML that reproduces this problem.

    1. Click on the 'Click me' link. The character range 5-15 should be selected.
    2. Click on the 'Click me 2' link. The character range 0-10 should be selected.
    3. Click on the text area. The selection should disappear (expected behavior).
    4. Click on the 'Click me' link again. The character range 5-15 SHOULD be selected, but it is not.
    5. Click on the 'Click me 2' link again. The character range 0-10 SHOULD be selected, but it is not.

    What is it that I am missing? Any help would be greatly appreciated.

    Code:
    <html>
    	<head>
    		<title>Test Page</title>
    	</head>
    	<body>
    		<form name="aForm" id="aForm" action="" method="POST">
    			<textarea name="aTextArea" id="aTextArea">This is my text area.</textarea>
    		</form>
    		<a href="#" onclick="document.getElementById('aTextArea').setSelectionRange(5,15); return false;">Click me.</a>
    		<br />
    		<a href="#" onclick="document.getElementById('aTextArea').setSelectionRange(0,10); return false;">Click me 2.</a>
    	</body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I see what you mean. This works here in FF 3:

    Code:
    <html>
    	<head>
    		<title>Test Page</title>
    <script type="text/javascript">
    function setrange(id, a, b){
    var el = document.getElementById(id);
    el.focus();
    el.setSelectionRange(a, b);
    }
    </script>
    	</head>
    	<body>
    		<form name="aForm" id="aForm" action="" method="POST">
    			<textarea name="aTextArea" id="aTextArea">This is my text area.</textarea>
    		</form>
    		<a href="#" onclick="setrange('aTextArea', 5, 15); return false;">Click me.</a>
    		<br />
    		<a href="#" onclick="setrange('aTextArea', 0 ,10); return false;">Click me 2.</a>
    	</body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •