Click here to Skip to main content
13,398,529 members (34,766 online)
Rate this:
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

<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
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 18:30pm
Updated 4-Nov-12 18:36pm
Why?! Better review your design.
Rate this: bad
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:[^].

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:[^],[^],[^].

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?

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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

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

Solution 2


Try this if could help...

$(document).ready(function() {
$("#mytextbox").focus(function() {
if($("#mytextbox").val() != "")

Do not forget to mark as answer if could help...

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180216.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2018
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