...

View Full Version : Problem with setSelectionRange in IE



MRMAN
06-30-2006, 02:17 PM
hello i have this very simple code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>boo</title>
<script language="javascript">

function movemiddle () {
var control = document.getElementById("textbox360");
control.focus();
control.setSelectionRange(5, 5);

}
</script>
</head>
<body>

<input id="textbox360" type="text"><input type="button" onClick="movemiddle()">
</body>
</html>



wen i run it in mozilla the caret jumps to the correct place. But in IE i get

"A Runtime Error has occured.
Do you wish to Debug?

Line:11
Error: Opject doesn't support this property or method."

Does anyone know if this is becuase IE doesn't have the setSelectionRange function or am i don'tt something wrong.

Thank you for your time
MRMAN

CSTruter
06-30-2006, 03:38 PM
Try this?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>boo</title>
<script language="javascript">

function setSelectionRange(input, selectionStart, selectionEnd)

{
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionStart);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}


function movemiddle () {
var control = document.getElementById("textbox360");
setSelectionRange(control,5, 5);

}
</script>
</head>
<body>

<input id="textbox360" type="text"><input type="button" onClick="movemiddle()">
</body>
</html>

MRMAN
06-30-2006, 03:55 PM
Thank you very much. its fantastic.


Can i trouble you to ask how i can then get the chracters from the caret to the beginning of the string

so i would get MRM from MRM|AN

Thanks

jskaar
06-30-2006, 06:52 PM
This will at least point you in the correct direction: http://parentnode.org/javascript/working-with-the-cursor-position/

CSTruter
07-03-2006, 07:18 AM
Sorry for taking so long to reply, wasnt online this weekend...

You simply need to change a VERY small thing

in the setSelectionRange function, simply change the
first part to this:




if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}



You'll see that input.setSelectionRange changed a little.

Simply specify which part you want to select in your input string now

MRMAN
07-03-2006, 12:41 PM
Thank you very much for your help

BubikolRamios
12-28-2008, 06:30 PM
just corrected above code, thanks CSTruter



function setSelectionRange(input, selectionStart, selectionEnd)

{
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum