7 Replies Latest reply on Sep 1, 2010 12:57 PM by Dan Acuff

    How would I start to do a character count on a textarea?

    Dan Acuff Newbie

      I think I use a4j:support but I am not sure. I am new.

       

                       <rich:tab id="tshortDescription" label="#{messages.shortDescription}">
                              <s:decorate id="shortDescriptionDecoration"
                                  template="/layout/edit.xhtml">
                                  <ui:define name="label">#{messages.shortDescription}</ui:define>
                                  <h:inputTextarea id="shortDescription" cols="80" rows="3"
                                      value="#{itemHome.instance.longDescription}">

       

                                      <a:support event="onkeyup" reRender="echo, count"/>

       

                                  </h:inputTextarea>

       

                                  <h:outputLabel value="" for="" />

       

                              </s:decorate>
                          </rich:tab>

       

      I figured I would have an outputLabel to update with the actual character count.  How do I connect the dots?

      Do I absolutely need a backing bean to do the count on the textarea characters? Or is there allready a way via RichFaces or JSTL etc..?

       

      Thanks much.

        • 1. Re: How would I start to do a character count on a textarea?
          Siva P Master

          <h:inputTextarea id="shortDescription" cols="80" rows="3"
                                          value="#{itemHome.instance.longDescription}">

           

                                          <a:support event="onkeyup" reRender="echo, count"/>

           

                                      </h:inputTextarea>

           

                                      <h:outputLabel value="" for="" />

           

          In <a4j:support you have actionListener adnd call the backing bean and set the value for the h:outputLabel, specifiy the id for the h:outputLabel and reRender it in <a4j:support

           

           

           

          try like this ,

           

          reRender="p_descriptionField,eDescriptionField"

           

          <a4j:outputPanel id="p_descriptionField">
          <h:outputLabel id="eDescriptionField" value="beanname.property" for="shortDescription" />
          </a4j:outputPanel>
          1 of 1 people found this helpful
          • 2. Re: How would I start to do a character count on a textarea?
            Andreas Hoehmann Newbie
            Keep it simple

             

             

            <h:inputTextarea id="shortDescription" cols="80" rows="3"
                             value="#{itemHome.instance.longDescription}">
            <a:support event="onkeyup" reRender="count" limitToList="true"/>
            </h:inputTextarea>
            <h:outputLabel id="count" value="#{fn:length(itemHome.instance.longDescription)}"/>

             

            For the fn:length function see https://facelets.dev.java.net/nonav/docs/dev/docbook.html#taglib-available-jstl

             

             

            If you don't like the fn:length function you can put a other getter into your bean

             

             

            int getTextAreaLength() ... there you must calculate the length of the #{itemHome.instance.longDescription} value

             

            <h:outputLabel id="count" value="#{bean.textAreaLength}"/>

             

             

            Use limitToList=true to submit only this single text-area.

             

             

            At at least you can calculate the length of the text-area on client side via javascript.
            regards
            andreas
            • 3. Re: How would I start to do a character count on a textarea?
              Dan Acuff Newbie

              HTTP Status 503 - Concurrent call to conversation


              type Status report

              message Concurrent call to conversation

              description The requested service (Concurrent call to conversation) is not currently available.

               

              I tried the fn:length approach with a4j:support....

               

              You see the results... something about SEAM and the conversation scope I am guessing.

               

              I will have to try a backing bean or javascript.

               

              I really liked this first approach the best cause it was simplest!

               

              UPDATE: I was able to use the a4j:support attribute ignoreDupResponses = "true" and it solved the error problem.

              • 4. Re: How would I start to do a character count on a textarea?
                Ilya Shaikovsky Master

                I highly recommed to use some JS solution.. My concern is that having bunch of requests fired by keyup just to cound character in input(if you not need to update them at backend so often) - typical "too much ajax" case

                1 of 1 people found this helpful
                • 5. Re: How would I start to do a character count on a textarea?
                  Dan Acuff Newbie

                  Ilya Shaikovsky wrote:

                   

                  I highly recommed to use some JS solution.. My concern is that having bunch of requests fired by keyup just to cound character in input(if you not need to update them at backend so often) - typical "too much ajax" case

                  Thank you for your reply.

                  I first tried a javascript approach. But because my Id's look like this:

                   

                  <textarea rows="3" id="item:shortDescriptionDecoration:shortDescription">

                   

                  I can't get the javascript to regonize it as a valid field name.

                   

                  onkeydown="textCounter(document.item.item:shortDescriptionDecoration:shortDescription,document.item.item:shortDescriptionDecoration:remLen2,200)"
                  onkeyup="textCounter(document.item.item:shortDescriptionDecoration:shortDescription,document.item.item:shortDescriptionDecoration:remLen2,200)"

                   

                   

                  function textCounter(field,cntfield,maxlimit) {
                  if (field.value.length > maxlimit) // if too long...trim it!
                  field.value = field.value.substring(0, maxlimit);
                  // otherwise, update 'characters left' counter
                  else
                  cntfield.value = maxlimit - field.value.length;
                  }

                   

                  Any recommendations on how to get an ID that can be recognized as valid?

                  • 6. Re: How would I start to do a character count on a textarea?
                    Ilya Shaikovsky Master

                    you passing elements to function?

                     

                    then use:

                    onkeydown="textCounter(#{rich:element('shortDescription')},#{rich:element('remLen2'),200)"
                    • 7. Re: How would I start to do a character count on a textarea?
                      Dan Acuff Newbie

                      Ahhh your the best! Thank you, I did not know about the rich:element.

                       

                      Javascript counts the characters faster than the ajax can as well so I will stick with the JavaScript solution.