0

Im looking to achieve this https://www.primefaces.org/remaining-chars-for-inputtextarea/ but with an inputtext, but it doesn't have those attributes, im thinkg of putting a label down the inputtext that updates it self with every keydown event but I don't think thats the best way, any ideas? im using PF 3.5 by the way.

BugsOverflow
  • 386
  • 3
  • 19
  • 1
    Easiest way to do this is make some CSS changes, set resize:none to textarea it will become input text...and you can change size accordingly. – this.girish Feb 21 '20 at 22:11
  • Suggestion before is good, or add a simple jquery 'plugin' since this is already included in PrimeFaces or do plain javascript. Both visible in https://stackoverflow.com/questions/12742595/show-how-many-characters-remaining-in-a-html-text-box-using-javascript#12745387 – Kukeltje Feb 22 '20 at 09:28

1 Answers1

0

Try this:

<h:outputText id="char_track"
              value="#{10 - bean.value.length()} characters remaining."/>

<p:inputText value="#{bean.value}" maxlength="10">
  <p:ajax event="keyup" global="false" update="char_track"/>
</p:inputText>

The idea here is to update the h:outputText every time a character is added to the input field using keyup event in p:ajax in the input field. The length() method gives the length of String.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Noah
  • 68
  • 1
  • 7
  • Please explain what this code does? Also try to make a snippet for better understanding, Write in detail , don't just throw a random code . – this.girish Feb 21 '20 at 23:05
  • 1
    You should really think twice before you say **"random code"**. Have you tried this answer and seen if its random? – Noah Feb 21 '20 at 23:17
  • 1
    Don't use ajax for this... bad design – Kukeltje Feb 22 '20 at 09:27
  • 1
    it is not random code but it indeed missed an explanation. – Kukeltje Feb 22 '20 at 09:35
  • 2
    @Noah , i'm not here to test some code and test to validate, if your answering something Please explain briefly about problem and solution or before posting answer discuss with question owner, try this that and post some code will not help anyone now or in future, read how to write good answer https://stackoverflow.com/help/how-to-answer – this.girish Feb 22 '20 at 11:24
  • 1
    @bugsforbreakfast: read the comments on the question. Effective;y all pf the, work and are hence technically correct. This on with ajax is not a good approach unless a delay on the ajax is used – Kukeltje Feb 24 '20 at 14:22