jQuery: how to find first visible input/select/textarea excluding buttons?

Artem picture Artem · May 13, 2010 · Viewed 124.6k times · Source

I tried

$(":input:not(input[type=button],input[type=submit],button):visible:first")

but it doesn't find anything.

What is my mistake?

UPD: I execute this on $(document).load()

<script type="text/javascript">
$(window).load(function () {
  var aspForm  = $("form#aspnetForm");
  var firstInput = $(":input:not(input[type=button],input[type=submit],button):visible:first", aspForm);
  firstInput.focus();
});
</script>

and in the debug I can see that firstInput is empty.

UPD2: I'm in ASP.NET page running under Sharepoint.

I've found so far that for some elements it does find them (for fixed ones) and for some don't. :(

Answer

Mottie picture Mottie · May 13, 2010

Why not just target the ones you want (demo)?

$('form').find('input[type=text],textarea,select').filter(':visible:first');

Edit

Or use jQuery :input selector to filter form descendants.

$('form').find('*').filter(':input:visible:first');