Forum Moderators: open

Message Too Old, No Replies

Clearing specific form fields!

can I do this?

         

dreamcatcher

6:51 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Lets say I have a form and in that form are 10 input type text fields that would contain e-mail addresses and I want to have a button to clear those and nothing else? Like a reset button but only for specific fields.

Can I do this?

Thanks.

Bernard Marx

9:44 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. Not so hard, it all depends on what you are using to distinguish these textboxes from the other inputs.

1 Are there textboxes within the form that you don't want cleared?
2 Do these Tboxes have id's?

dreamcatcher

9:57 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Bernard. I finally got it:


<script language="javascript">

function reset_emails()
{
for (var i=0; i<document.friendform.elements.length; i++)
{
if (document.friendform.elements[i].name.indexOf("friend_email")!= -1)
{
document.friendform.elements[i].value = "";
}
}
return true;
}
</script>

:)

Bernard Marx

12:45 am on May 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool. I was asleep anyway!

It's probably worth mentioning that - just for the purposes of code clarity/efficiency - you could set a local variable at the top of the function,

var elements = document.friendform.elements

then simply use that instead throughout.

Also, if you are dealing with modern browsers only,

document.getElementsByName("friend_email")
will give you a collection of all of them.

dreamcatcher

7:04 am on May 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the insight Bernard. I have made a note of your comments for next time. :)