aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Java Script to clear text box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Java Script to clear text box" Watch "Java Script to clear text box" New topic
Author

Java Script to clear text box

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I didnt see a Java Script area but if there is one please move this post into it for me.

I have a simple form



I want to clear out the City Name text box when the user clicks the submit button. Ive seen javascripts that clear out the text box when the user clicks inside the textbox(onFocus) and I tried to make it work but could not. I would also like to have the form set the subject line for me too I used to know how in college, but have not used it awhile and got rid of the book. Help!
[ August 01, 2007: Message edited by: James Hambrick ]

Visit my blog! http://jameshambrick.com
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
this.value=''

Eric
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

this.value = "" on the button will clear the text on the button. I have to reference the textbox from the submit button.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6



Seems kind of like a strange thing to do though. The cityName will post with no value.


GenRocket - Experts at Building Test Data
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

After the data is sent I want the text box to be cleared not before.
Maybe I should just redirect them to a different page after clicking submit.
[ August 01, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Im searched and not found anything on how to redirect them after clicking the submit button. Any ideas how?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Originally posted by James Hambrick:
After the data is sent I want the text box to be cleared not before.
Maybe I should just redirect them to a different page after clicking submit.


What sense would it make to redirect them?

Use an onsubmit handler to clear the field when the form is submitted.
[ August 01, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

when the page reloads, it shows the text still in the texbox I was thinking of sending them to another page that may thank them for their input and assure them that someone is working on their request. I really just want the textbox to be blank after the user clicks submit. how do you use a onsubmit handler?
[ August 01, 2007: Message edited by: James Hambrick ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

See my revised reply (which I updated just as you were replying).
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

This is what I have and it does not work

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

"it does not work" is not a particularly useful description of the symptoms.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Your handler needs to return true to keep the event propagating.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

return true???

It does not work means that after I click the submit button and the data is sent the textbox is not clear, it still has the information that was typed into it.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

function doClear()
{

document.text_form.city.value = "";
return true;
}

it returns true and still does not clear the textbox after clicking submit, the textbox still has the information that user typed into it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

document.text_form.city.value = "";


This doesn't look like what Gregg suggested.

Is the function being called at all?

What debugging tools are you using to help you debug your JavaScript?
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

didn't know there was a debugging tool for javascript. I did not try what Gregg suggested because I think it said somewhere that if I do that, it will clear the text box before the information is sent. Seems like after submitting the data it the textbox would automatically clear itself after the refresh. Would save alot of trouble.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Originally posted by James Hambrick:
didn't know there was a debugging tool for javascript.


There are a number of them. I mostly use FireBug for FireFox.

I did not try what Gregg suggested because I think it said somewhere that if I do that, it will clear the text box before the information is sent.


How you reference the element has no bearing on when it will be cleared. Using document.getElementById() is the surest way to make sure you are referencing the element correctly.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

yep it cleared it right before it sent out the email so my email said
cityname = " "
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

I take it then that you want it cleared after the submission? Using a mailto as a form submission target is rather a weird deal. Normally a submission causes a page refresh and so there is no native mechanism for an after-submit event.

You could try clearing the field in a timeout handler initiated by the onsubmit event, but I'm a bit uncomfortable with that solution as it could lead to a race condition.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Script to clear text box