Connecting Tech Pros Worldwide Forums | Help | Site Map

How to unselect a text of an input box

Stefan Mueller
Guest
 
Posts: n/a
#1: Nov 27 '05
I've an input box
<input type = "text" name = "MyInput" value = "">

and a selection
<select name = "MySelection" size = "1">
<option value = "1">Entry 1
<option value = "2">Entry 2
</select>

With
document.MyForm.MyInput.focus();
document.MyForm.MyInput.select();

I can set the focus to the input box and select the whole text. But if I set
the focus to the selection box with
document.MyForm.MySelection.focus();

the text in the input box is still selected.

How can I unselect this text?
Stefan

PS: One possibility would be to add a hidden input box and to set the focus
to this hidden box before I set the focus to the selection. But I think this
would only be a workaround.



McKirahan
Guest
 
Posts: n/a
#2: Nov 27 '05

re: How to unselect a text of an input box


"Stefan Mueller" <seekware-remove-@yahoo.com> wrote in message
news:dmch7p$2buf$1@news.imp.ch...[color=blue]
> I've an input box
> <input type = "text" name = "MyInput" value = "">
>
> and a selection
> <select name = "MySelection" size = "1">
> <option value = "1">Entry 1
> <option value = "2">Entry 2
> </select>
>
> With
> document.MyForm.MyInput.focus();
> document.MyForm.MyInput.select();
>
> I can set the focus to the input box and select the whole text. But if I[/color]
set[color=blue]
> the focus to the selection box with
> document.MyForm.MySelection.focus();
>
> the text in the input box is still selected.
>
> How can I unselect this text?
> Stefan
>
> PS: One possibility would be to add a hidden input box and to set the[/color]
focus[color=blue]
> to this hidden box before I set the focus to the selection. But I think[/color]
this[color=blue]
> would only be a workaround.
>[/color]

Will this help?

<html>
<head>
<title>blur.html</title>
<script type="text/javascript">
function onloader() {
document.MyForm.MyInput.focus();
document.MyForm.MyInput.select();
}
function clearSelection () {
if (document.selection)
document.selection.empty();
else if (window.getSelection)
window.getSelection().removeAllRanges();
}
</script>
</head>
<body onload="onloader()">
<form action="" method="get" name="MyForm">
<input type="text" name="MyInput"
value="Hello World" onblur="clearSelection()">
<select name="MySelection" size="1">
<option value="1">Entry 1
<option value="2">Entry 2
</select>
</form>
</body>
</html>


Credit
URL:http://groups.google.com/group/comp...._thread/thread
/871226cbeb74ae70/d3a48c14f66cc707?lnk=st&q=JavaScript+deselect+text &rnum=8&
hl=en#d3a48c14f66cc707


Stefan Mueller
Guest
 
Posts: n/a
#3: Nov 27 '05

re: How to unselect a text of an input box


> function clearSelection () {[color=blue]
> if (document.selection)
> document.selection.empty();
> else if (window.getSelection)
> window.getSelection().removeAllRanges();
> }[/color]

With Internet Explorer and Firefox is works, but not with Opera.

Opera error message:

Event thread: blur
Error:
name: TypeError
message: Statement on line 11: Type mismatch (usually a non-object value
used where an object is required)
Backtrace:
Line 11 of inline#1 script in file://localhost/D:/test.html
document.selection.empty();
Line 1 of script
clearSelection();
At unknown location
[statement source code not available]


Stefan Mueller
Guest
 
Posts: n/a
#4: Nov 30 '05

re: How to unselect a text of an input box


> With Internet Explorer and Firefox is works, but not with Opera.

I've tried many solutions to unselect a text of an input box. To only
solution I found which works on all browsers is

var temptext = document.MyForm.MyInput.value;
document.MyForm.MyInput.value = "";
document.MyForm.MyInput.value = temptext;

Stefan


Closed Thread


Similar JavaScript / Ajax / DHTML bytes