Thread: PreventDefault

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Baltimore
    Posts
    185
    Rep Power
    15

    PreventDefault


    I have it so that when the characters in the box get to a certain number that you can no longer type. When I hit the limit and then erase some characters I still can't type. I really need some help with this.

    Code:
    if(charCount >= 30) {
                    $('#'+ editorInstance.name).text(function (_,txt) {      
                        return txt.slice(0, 30); 
                    });
                   $('#'+ editorInstance.name).keydown(function(event) {
                    if (event.keyCode > 27) {
                    event.preventDefault();
    
                        } 
                    });
                }
    Thanks in advance
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this doesn't make it lot of sense. It seems that every time there are more than 30 characters, you add a new event handler. This means you end up with tens or hundreds of event handlers all blocking the field.

    This whole approach looks like a half-baked emulation of the maxlength attribute. Why don't you simply use that attribute? It requires no JavaScript magic, and it actually works. For example, I guess you've not considered that people can also paste text without hitting any key.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Baltimore
    Posts
    185
    Rep Power
    15
    I'm doing this function with ckeditor, there is no maxlength field. Even if you paste inside of this it will still cut off the text to 30 characters...that stuff is already working. The problem i am having is that once it stop you from typing and it cuts you off at 30 characters then when you erase character you should be able to type in more characters up to 30 again but its not letting me type anything in the box at all but it will let me erase.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by FredS
    I'm doing this function with ckeditor, there is no maxlength field.
    You can set the maxlength attribute with JavaScript. This reduces all your code to a single line:

    javascript Code:
    your_input_element.attr('maxlength', 30);




    Originally Posted by FredS
    that stuff is already working.
    I guess we have a different understanding of "working".



    Originally Posted by FredS
    The problem i am having is that once it stop you from typing and it cuts you off at 30 characters then when you erase character you should be able to type in more characters up to 30 again but its not letting me type anything in the box at all but it will let me erase.
    And I told you why that is.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    FredS, perhaps this will help you: http://stackoverflow.com/questions/2...nteditable-div

    Originally Posted by Jacques1
    You can set the maxlength attribute with JavaScript. This reduces all your code to a single line:

    javascript Code:
    your_input_element.attr('maxlength', 30);
    That's an attribute of form controls, but ckeditor and similar scripts use contentEditable and/or designMode to turn other elements, e.g. an iframe, into what could be called an "enhanced textarea".

    Jacques1, would you please stop acting like you know everything?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo