2 Replies Latest reply on Sep 8, 2008 9:01 AM by Christopher Ljungblad

    focus (highlight problem) after ajax request

    Christopher Ljungblad Newbie


      I am trying to get focus on a inputTextarea after an ajaxRequest as shown below. The problem I get is that all text in the inputTextarea is highlighted afterwards. Anyone know how to fix this? That is to get rid of the highlight and also set the cursor to the end of the inputTextarea.

      <a4j:form id="commandInputbox">
       <h:inputTextarea id="commInputboxTA"
       style="width : 469px; height : 500px;"
       <a4j:support event="onkeyup"
      action="#{commandHelper.parseCommands}" reRender="commInputboxTA"
       focus="commInputboxTA" />


        • 1. Re: focus (highlight problem) after ajax request
          Andrei Markavtsov Apprentice


          This is a known issue. It will be fixed in future version.
          Now please use the following workaround:

          1. Add JS function to the page:

          <script type="text/javascript">
           function setCaretToEnd (e) {
           var control = $((e.target ? e.target : e.srcElement).id);
           if (control.createTextRange) {
           var range = control.createTextRange();
           else if (control.setSelectionRange) {
           var length = control.value.length;
           control.setSelectionRange(length, length);
           control.selectionStart = control.selectionEnd = control.value.length;

          2. Remove focus attribute. Assign the function on 'oncomplete' event:

          <a4j:support event="onkeyup" ... oncomplete="setCaretToEnd(event);"/>

          • 2. Re: focus (highlight problem) after ajax request
            Christopher Ljungblad Newbie

            Terrific, thanks!! Now it "works" in IE and partly in FF.

            In FF, the cursor is set to the correct position. But if the inputTextArea has some text in it and have become "scrollable" I have to scroll down to the end of the textArea.

            In IE focus will stay put at the end of the inputTextArea after reRender.

            Tried this in: IE7.0.5730.13 and FF