473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 39742
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Navarre | last post by:
Hello, I have noticed that when using JavaScript to create a page, the sizes of the text input form elements come out bigger than the size I am trying to set them to. I don't know if it...
2
by: Kai Grossjohann | last post by:
I would like to put a text input field (in the sense of <input type="text">) and an image next to each other, where I know the size in pixels of the image, and I know the total width in em. I...
8
by: BiNZGi | last post by:
Hi I have reduced the problem to this code: <form> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td><input type="text" style="width: 100%;" value="Lorem ipsum dolor...
5
by: Art | last post by:
Hi, I'm using a form text input to display a JavaScript clock. It has no other purpose than that, just show the date and time. What I would like to do now is prevent anything else happening when...
4
by: ms | last post by:
I have come across a problem where if a user enters the same text into a text input after the value has been programmatically changed, the onChange event does not fire for the second change. The...
1
by: NullBock | last post by:
Hi, I'm trying to create a form with a text-input (not textarea) that expands to the size available, using CSS. I thought that a simple display:block would work: <form> <div> <input...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
4
by: Andrew Poulos | last post by:
I have a form that looks something like the following: <form"> <div id="pg0"> <!-- more HTML here --> <input type="text" name="f0_0" > </div> <div id="pg1" style="display:none;"> <!-- more...
11
by: Mike Harrison | last post by:
Hi, I have some simple HTML like this: <div id="container" style="width:100%;"> <input type="text" <input type="button" style="float:right;" value="Click here..."> </div> I want the button...
0
by: pazazuzu | last post by:
Hi everyone, I currently have an assignment which I am almost done with but am having problems trying to figure out how to integrate a string function search. I have a text box called...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.