Search code examples
jquerycssjeditable

CSS / jQuery - Limit the number of characters for all dynamically generated textbox


I am using jEditable for inline edit, so when user double click a text, it will turns into a textbox.

I need to limit the number characters users can enter into the textbox, but since the textbox is generated from the plugin, I could not use the inline styling like this:

 <input type="text" name="name" maxlength="100" />

Any idea how I can style all textboxes in css so user cannot input more than 30 characters?

Thanks a lot...


Solution

  • I'm a bit confused. If you're already using jQuery, is there a reason not to use it to limit the length?

    http://jsfiddle.net/rkw79/TvDvd/2/

    $('body').delegate('input[type="text"]','focus', function() {
        $(this).attr('maxLength',100);
    });
    

    Limit text areas also, just incase jEditable uses that: http://jsfiddle.net/rkw79/TvDvd/3/

    $('body').delegate('input[type="text"], textarea','focus', function() {
        $(this).attr('maxLength',100);
    });