Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a textbox ,i want whenever user start entering a number in that texbox the previous value should be remove.
 
Let say there is a textbox which contain a number as 23 ,i want whenever user start typing in that same textbox 23 shold be remove and new enetered number should be shown.
 
My code as follow
document.getElementById("mytextbox").select();
$("#mytextbox").select();
 
<input type="text" onclick="clear();">
 
 
But still my code is not working,i am using ie9 ,window 7 64 bit pc,i debug the code and did quickwatch to this line
 document.getElementById("mytextbox").select()  
it is showing me undefined.,I am able to find the mytextbox.
Is there any other way to do this.My code is working in chrome and mozilla.
Posted 4-Nov-12 17:30pm
Edited 4-Nov-12 17:36pm
v2
Comments
Sergey Alexandrovich Kryukov at 4-Nov-12 23:48pm
   
Why?! Better review your design.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To start with, if you are already using jQuery, document.getElementById is not needed, as you can use jQuery selectors:
http://api.jquery.com/category/selectors/[^].
 
Now the solution: text box is actually the input element with the attribute type="text". For all input elements, its value is accessed through its property value:
http://www.w3schools.com/tags/tag_input.asp[^],
http://www.w3schools.com/tags/att_input_value.asp[^],
http://www.w3schools.com/jsref/prop_option_value.asp[^].
 
So, apparently, you only need to assign this value to empty string, in the handler of the event you need under condition you need.
 
However, I would not advise you to do it. Usually, the user knows better what to delete from input data and when. By the user will be only frustrated if the input data is cleaned without her/his order. Maybe the user wants to repeat the same value or modify just one or too characters in it? If I, personally, came across the behavior you describe, I would do all my best to avoid your site. Some of your users might feel the same. Do you need it?
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this its working fine Smile | :)
<script type="text/javascript">
	    $(function () {
	        $("#mytext").focus(function () {
	            var keyPressed = false;
	            $("#mytext").keypress(function () {
	                // alert("Handler for .keypress() called.");
	                keyPressed = true;
	            });
                if(keyPressed)
	            $("#mytext").val("");
	        });
	    });
        
	</script>
 

<body>
<div class="post">
	<input type="text" id="mytext" />
        <input type="text" id="urtext" />
	</div>
   
</body>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Try this if could help...
 
$(document).ready(function() {
$("#mytextbox").focus(function() {
if($("#mytextbox").val() != "")
   $("#mytextbox").val("");
 });
});
 

Do not forget to mark as answer if could help...
 
Regards,
  Permalink  
Comments
aassaahh at 5-Nov-12 12:45pm
   
when user starts typing that time the value should be clear,not when cursor of the mouse is placed on the control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Richard MacCutchan 240
2 OriginalGriff 230
3 Jared Sanow 145
4 Vinay Mistry 143
0 Sergey Alexandrovich Kryukov 5,955
1 OriginalGriff 4,805
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100