martok
martok

Reputation: 479

jQuery selectors - Skipping certain input

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.

Upvotes: 1

Views: 56

Answers (3)

Ariel
Ariel

Reputation: 26783

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

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

Upvotes: 0

Thor Jacobsen
Thor Jacobsen

Reputation: 8861

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

Upvotes: 0

BoltClock
BoltClock

Reputation: 724542

Sure, instead of

$(':input', form)

Use

$(':input:visible', form)

Upvotes: 3

Related Questions