Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
2.00/5 (1 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
Updated 4-Nov-12 17:36pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Nov-12 23:48pm    
Why?! Better review your design.
--SA

Try this its working fine :-)
JavaScript
<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>




HTML
<div class="post">
	<input type="text" id="mytext" />
        <input type="text" id="urtext" />
	</div>
 
Share this answer
 
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,
 
Share this answer
 
Comments
aassaahh 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.
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900