Announcement

Collapse
No announcement yet.

Javascript Character Counter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Character Counter

    Here a few little functions I made to use with a form, to tell the user how many characters they have left. Obviously, they can bypass this without JS turned off, so make sure to check the length of the submitted string, server-side also!



    Usage:

    HTML Code:
    <form action="#" method="POST" id="dummy_form">
        <textarea row="5" cols="60" id="dummy_text" onkeyup="characterCounter('dummy_text', 'counter', 300);" onkeydown="characterCounter('dummy_text', 'counter', 300);"> Heya MakeWebGames </textarea>
        <input type="submit" id="submit" name="submit" value="Submit" />
    </form>
    <span id="counter"></span>
    
    <script type="text/javascript">
        fillCounter('dummy_text', 'counter', 300);
    </script>
    If you need help or need something made, PM me.


  • #2
    havent used it but it seems pretty good

    Comment


    • #3
      mdshare was kind enough to lend me some server space, and I set up an example:

      http://www.browsergames.be/counter_example.html
      If you need help or need something made, PM me.

      Comment


      • #4
        Kudos for posting that code blueDevil. Might I suggest packaging it as an object? That would allow the function names to be cleaned up a bit.

        Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

        Comment


        • #5
          So far, I didn't encountered any "modern" browsers which doesn't support the "getElementById" function. So your first function seems a bit just too much work for nothing. Also, why have it on "onKeyDown" and "onKeyUp" event? Why not simply onKeyPress or onKeyUp alone?
          - Make Web Games
          - Creator of NWE
          - Owner of Nowhere Else and beyond
          - Mad developer

          Comment


          • #6
            @Floydian

            Thanks for the tips. Javascript is really an area I would like to improve in.

            @Alain

            Internet Explorer has problems with getElementById().

            And on the event... meh either of your proposed choices would be fine. I have no real reason I did it, just for completeness.
            If you need help or need something made, PM me.

            Comment


            • #7
              IE doesn't have ANY problem with getElementById, and I can assure you about it as I always use getElementById and my game fully works with both IE and FF.
              - Make Web Games
              - Creator of NWE
              - Owner of Nowhere Else and beyond
              - Mad developer

              Comment


              • #8
                IE 8 doesn't (I believe). IE 6 and 7 search for both ID *and* the name attribute when using getElementById(). The easy way to get around that is not using the same ID and the same name on different elements.... but that's not always the case.
                If you need help or need something made, PM me.

                Comment


                • #9
                  Sorry but who would use the same NAME and ID on 2 different objects? Never saw it, never had it... so that's why for me getElementById always worked

                  Anyhow thanks for pointing me this issue as I was totally unaware of it. Even if maybe I will not need it now, you never knows.
                  - Make Web Games
                  - Creator of NWE
                  - Owner of Nowhere Else and beyond
                  - Mad developer

                  Comment


                  • #10
                    Not the same ID *and* name on different objects, but say you have a form... you give the form the an ID of foo... but then end up naming one of the input foo... it's going to create problems.
                    If you need help or need something made, PM me.

                    Comment


                    • #11
                      Yes that's what I meant
                      - Make Web Games
                      - Creator of NWE
                      - Owner of Nowhere Else and beyond
                      - Mad developer

                      Comment


                      • #12
                        Originally posted by 'BlueDevil23',index.php?page=Thread&postID=150479# post150479
                        Not the same ID *and* name on different objects, but say you have a form... you give the form the an ID of foo... but then end up naming one of the input foo... it's going to create problems.
                        And that's an excellent reason, in general, to use a javascript library since these browser incompatibilities are taken care of for you.
                        Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

                        Comment

                        Working...
                        X