1

I have the following function for clearing input forms:

function clearForm(form) {
  $(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase();

    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = "";
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

Is there any way I can stop it from clearing inputs which are 'hidden'?

Thanks in advance.

3 Answers 3

3

Sure, instead of

$(':input', form)

Use

$(':input:visible', form)
Sign up to request clarification or add additional context in comments.

Comments

0

$(':input:visible', form).each(...

Comments

0
$(':input:visible').val([])

You don't need any of the code in the each function, the single line above will do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.