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

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


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');


The JQuery code is fine. You must execute in the ready handler not in the window load event.

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

Update

I tried with the example of Karim79(thanks for the example) and it works fine: http://jsfiddle.net/2sMfU/


This is my summary of the above and works perfectly for me. Thanks for the info!

<script language='javascript' type='text/javascript'>    $(document).ready(function () {        var firstInput = $('form').find('input[type=text],input[type=password],input[type=radio],input[type=checkbox],textarea,select').filter(':visible:first');        if (firstInput != null) {            firstInput.focus();        }    });</script>