开发者

How to set focus to a textbox on page load event

开发者 https://www.devze.com 2022-12-28 02:43 出处:网络
I am trying to set focus to the text box on page load event. I tried many solutions by referring the element id, but couldn\'t able to focus the element. Lat开发者_如何学Cer when I inspect the element

I am trying to set focus to the text box on page load event. I tried many solutions by referring the element id, but couldn't able to focus the element. Lat开发者_如何学Cer when I inspect the element using firebug I found for the same element the id changes in different execution. I am looking for the simplest solution using javascript or jquery to achieve this

    <h:form id="form"> 
      <rich:dataTable value="#{books}" var="book">
        <ui:repeat value="#{authors}" var="author"> 
          <h:inputText value="#{author.name}"/>
        </ui:repeat>
      </rich:dataTable>
</h:form>


$(document).ready(function(){
  $('input.selected').focus();
});    

Should focus the input element with the class 'selected' on page load.


Use

$('#form :input:visible:enabled:first').focus();

during document ready.


Using jquery, this will grab the first input child of the #form:

$('#form input:eq(0)').focus();


According to whatwg.org the HTML5 autofocus attribute is only available in Opera at the moment but will be adopted by other browsers in the future.

<input type="text" autofocus />

<script>
    // jQuery
    $(function(){
        if(!("autofocus" in document.createElement("input"))){
            $("input[autofocus]:eq(0)").focus();
        }
    });
</script>


Private Sub SetFocus(ByVal ctrl As Control)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javascript'>" & _
"document.getElementById('" + ctrl.ClientID & _
"').focus();</script>"

' Add the JavaScript code to the page.
Page.RegisterStartupScript("FocusScript", focusScript)
End Sub

hope this will help you..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号