PDA

View Full Version : onsubmit: clearing a textarea


DeadlySin3
02-06-2005, 03:13 AM
Hello,

i'm curious as to how I could go about clearing a textarea in a form on submit. A buddy of mine gave me this little snippet; but i've had no luck in getting it to work. Think someone could explain to me what it is i'm doing wrong?


<SCRIPT TYPE="text/javascript"><!--
onload = function() {
tagForm = document.forms["TagItv2"];
tagForm.onsubmit = function() {
/* execute the 'empty textarea' statement 1sec after the click on
submit so the content of the textarea is submited
before deleted */
setTimeout("tagForm[\"tagitpost\"].value = \"\"), 1000");
return true;
};
};//-->// -->
</SCRIPT>


I have the above placed in the head section of my page. TagItv2 is the name of my form and tagitpost is the name of the textarea field. I'm not getting any errors, but the textarea field isn't cleared after the form is submitted. If you need to know anything else that I may have left out please let me know.

Any help with this is greatly appreciated!

iDxMan
02-06-2005, 12:15 PM
Just curious: what's the purpose? I can see clearing fields onsubmit that you don't want the backend processor to receive, but clearing during a submit seems like a waste.

-r

DeadlySin3
02-06-2005, 11:38 PM
The purpose is not to clear during a submit, but 1 second after. so that way after the form is submitted, the data is processed then cleared from teh textarea.

Either way, this issue is solved. Thanks Seetle!


<SCRIPT TYPE="text/javascript"><!--
onload = function() {
tagForm = document.forms["TagItv2"];
tagForm.onsubmit = function() {
/* execute the 'empty textarea' statement 1sec after the click on
submit so the content of the textarea is submited
before deleted */
setTimeout("tagForm.tagitpost.value = '', 1000");
return true;
};
};//-->// -->
</SCRIPT>

Viper007Bond
02-07-2005, 02:32 AM
Why not just reload the page?

iDxMan
02-07-2005, 10:40 PM
Why not just reload the page?

Exactly.. (unless that's the point we missed/was left out)

Submit the page, process the request then do whatever..

The next question that pops to mind is: what if the server is overloaded so it takes more than one second to receive your POSTed data? Sounds like the field might be nuked before the request made it. (eg: impatient user hits stop then tries again, etc..)

-r

Viper007Bond
02-08-2005, 01:33 AM
Plus, it's best to rely on server side scripting rather than client side IMO. What if the user has Javascript disabled?

I rarely use Javascript and stuff and when I do, it's only for prettiness / non-required stuff.

For example, pretty font headers on my site: http://www.viper007bond.com/

DeadlySin3
02-08-2005, 10:46 PM
All very good suggestions/thoughts/ideas.

I wanted this bit of code because i'm writing a Tagboard script. All the information input on the form gets pushed through an i-frame for processing so refreshing the main page seems like a waste as the i-frame refreshes with up-to-date content right away.

Given that the user has javascript disabled, not a huge loss. They'll just have to reset the form via the clear button instead of having the textarea cleared for them with the javascript.

I've already written a flood control feature for the board and by default, it's set to not allow a user to submit the form more than 1 time in a 20 second period. I could always set the javascript to use that time limit to clear the text area.

Aside from that, this isn't a huge, serious project. It's just something that i'm toying around with and trying to make it easy as possible to use, yet still be useful.

Either way, if you have any better suggestions/ideas, please do share them w/me! You can see this script here: http://www.deadlysin3.net/tagit2beta