469,929 Members | 2,070 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

How to unselect a text of an input box

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.
Nov 27 '05 #1
3 36402
"Stefan Mueller" <se**************@yahoo.com> wrote in message
news:dm***********@news.imp.ch...
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.


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
Nov 27 '05 #2
> function clearSelection () {
if (document.selection)
document.selection.empty();
else if (window.getSelection)
window.getSelection().removeAllRanges();
}


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]
Nov 27 '05 #3
> 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
Nov 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by BiNZGi | last post: by
5 posts views Thread by Art | last post: by
4 posts views Thread by ms | last post: by
1 post views Thread by NullBock | last post: by
4 posts views Thread by Andrew Poulos | last post: by
11 posts views Thread by Mike Harrison | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.