Search code examples
javascriptjqueryhtmlchildren

Loop through visible input elements in a form in Javascript?


I'm trying to loop through all visible inputs within a form and set their value to be empty. What I have doesn't seem to work for text inputs, it returns undefined. Any ideas how to do this?


Solution

  • jQuery has a :visible selector as well as an :input selector. In addition, most jQuery methods operate on the entire set. val() can be used directly rather than looping through the set.

    currentForm.find(':input:visible').val('');