INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Reset input and select form values

Reset input and select form values

(OP)
Hello, I have some working JS code on my website that shows/hides DIVs.

CODE

function show1() {
   var div_num = $("#choosediv").val();
   if (div_num == 0) {
   $("#div1").hide(); 
   $("#div2").hide(); 
      $(':input', '#div1').each(function () {
   this.value = '';
   });
      $(':input', '#div2').each(function () {
   this.value = '';
   });
	};
   if (div_num == 1) {
   $("#div2").hide(); 
   $("#div1").show(); 
   $(':input', '#div2').each(function () {
   this.value = '';
   });
   	};
	   if (div_num == 2) {
   $("#div2").show(); 
   $("#div1").hide();
   $(':input', '#div1').each(function () {
   this.value = '';
   });
	};
} 

The line I'm interested in is:

CODE

$(':input', '#div1').each(function () {
   this.value = ''; 

What this line does is reset all form values in the shape of <input> fields.
At the same time I would like that line to reset all form values in the shape of <select> fields as well.

I have tried

CODE

$(':input', ':select', '#div2').each(function () {
   this.value = ''; 

But that doesn't work.
Rather than going in circles (my JS and syntax related knowledge is WEAK) I thought I'd post it here and see if anybody can help.

Thanks

RE: Reset input and select form values


2 Things:

1. There is no such thing as a :select pseudo selector for select dropdowns. The :input selector already gets text inputs, textures, and select boxes and buttons.
https://api.jquery.com/input-selector/

2. Select fields are a bit more complex than straight text inputs.

You need to actually select a value from their available options to change their value. IE. you can't just assign a blank value to them, you have to make one of their options be the selected option.

So you need change your code and add a bit more logic to it as well, to identify if its a text input or a select.

CODE

$(':input', '#div2').each(function () {
if(this.tagName == "SELECT")
{
   this.options[0].selected="selected";
}
else
{
	this.value="";
}
}); 

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Reset input and select form values

Why not simply use formname.reset() instead of using jQuery spaghetti hoops.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close