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

    focus (highlight problem) after ajax request

    Christopher Ljungblad Newbie

      Hi,

      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;"
       value="#{commandHelper.command}">
       <a4j:support event="onkeyup"
      action="#{commandHelper.parseCommands}" reRender="commInputboxTA"
       focus="commInputboxTA" />
       </h:inputTextarea>
       <rich:separator></rich:separator>
      </a4j:form>


      Thanks
      /Chris

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

          Hi,

          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();
           range.collapse(false);
           range.select();
           }
           else if (control.setSelectionRange) {
           control.focus();
           var length = control.value.length;
           control.setSelectionRange(length, length);
           }
           control.selectionStart = control.selectionEnd = control.value.length;
           }
          
           </script>


          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 2.0.0.16

            /Chris