Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 2.08 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to set focus to first editable input element in form
  2. <form style='margin: 30px' id="Form" class='form-fields' method='post' target='_blank'
  3.     action='Report/Render'>
  4. ...
  5.     <input id='_submit' type='submit' value='Show report' class='button blue bigrounded' />
  6.     </form>
  7.     <script type="text/javascript">
  8.         $(function () {
  9.             var elements = $('#Form').find(':text,:radio,:checkbox,select,textarea');
  10.             elements[0].focus();
  11.             elements[0].select();
  12. });
  13. </script>
  14.        
  15. $(function () {
  16.   $("#form :input:not([readonly='readonly']):not([disabled='disabled'])").first()
  17.                                                                         .focus();
  18. });
  19.        
  20. var elements = $('#Form').find(':text,:radio,:checkbox,select,textarea').filter(function(){
  21.     return !this.readOnly &&
  22.            !this.disabled &&
  23.            $(this).parentsUntil('form', 'div').css('display') != "none";
  24. });
  25. elements.focus().select();
  26.        
  27. $('#Form').find(':text,:radio,:checkbox,select,textarea').each(function(){
  28.     if(!this.readOnly && !this.disabled &&
  29.                 $(this).parentsUntil('form', 'div').css('display') != "none") {
  30.         this.focus();  //Dom method
  31.         this.select(); //Dom method
  32.         return false;
  33.     }
  34. });
  35.        
  36. var elements = $("#form").find("*").filter(function(){
  37.    if(/^select|textarea|input$/i.test(this.tagName)) { //not-null
  38.        //Optionally, filter the same elements as above
  39.        if(/^input$/i.test(this.tagName) && !/^checkbox|radio|text$/i.test(this.type)){
  40.            // Not the right input element
  41.            return false;
  42.        }
  43.        return !this.readOnly &&
  44.               !this.disabled &&
  45.               $(this).parentsUntil('form', 'div').css('display') != "none";
  46.    }
  47.    return false;
  48. });
  49.        
  50. $("#myForm :input:not([readonly='readonly']):not([disabled='disabled']):reallyvisible").first()
  51.                                                                                       .focus();
  52.        
  53. jQuery.extend(
  54.   jQuery.expr[ ":" ],
  55.   { reallyvisible : function (a) { return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); }}
  56. );