setSelectionRange.htm (2193B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title id="desc">HTML5 Selection: Call setSelectionRange() on a text field</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="common.js"></script> 8 <script type="text/javascript"> 9 function RunTest() 10 { 11 var selection = window.getSelection(); 12 var input1 = document.getElementById("input1"); 13 var expectedResult = "input"; 14 15 assert_equals(input1.selectionStart, 0); 16 assert_equals(input1.selectionEnd, 0); 17 checkDefaultSelectionAttributes(); 18 assert_equals(selection.toString(), ""); 19 20 // only calling setSelectionRange() doesn't affect getSelection() 21 22 input1.setSelectionRange(16, 21); 23 24 assert_equals(input1.selectionStart, 16); 25 assert_equals(input1.selectionEnd, 21); 26 checkDefaultSelectionAttributes(); 27 assert_equals(selection.toString(), ""); 28 29 // select() changes selectionStart/End and getSelection() 30 31 input1.select(); 32 33 assert_equals(input1.selectionStart, 0); 34 assert_equals(input1.selectionEnd, input1.value.length); 35 checkSelectionAttributes(document.body, 1, document.body, 1, true, 1); 36 assert_equals(selection.toString(), input1.value); 37 38 // now calling setSelectionRange() does affect getSelection() 39 40 input1.setSelectionRange(16, 21); 41 42 assert_equals(input1.selectionStart, 16); 43 assert_equals(input1.selectionEnd, 21); 44 checkSelectionAttributes(document.body, 1, document.body, 1, true, 1); 45 assert_equals(selection.toString(), "input"); 46 } 47 </script> 48 </head> 49 <body onload="test(RunTest);"> 50 <input style="WIDTH: 500px" id="input1" value="Some text in an input control" type="text" /> 51 <p>Call setSelectionRange() on the input element to select some of the text</p> 52 </body> 53 </html>