SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    textarea auto expand need help much! :(

    I followed this article from Sitepoint
    How to Build an Auto-Expanding Textarea jQuery Plugin, Part 2 SitePoint

    and everything is working fine until I added padding:3px to my textarea. In Google Chrome, now everything I type seems to auto expand the box no matter what is the height.

    Example: Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)

    Can anyone shed some light? Thanks in advanced.
    I Dunno LOL \(_o)/

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Have a look at these elementView properties

    The scrollHeight includes the padding, so what you can do is to make use of one of the elementView properties, that being clientHeight, which gives you the height of the element including the padding.

    Because both measurements include the padding, taking the difference between them removes the impact of the padding. The difference between scrollHeight and clientHeight If no change is required, will be 0. If the scrollHeight is 10 pixels larger than the clientHeight, then the box height needs to be 10 pixels larger.

    How to achieve that? jQuery has a .height() method that gives you the actual height of the box, minus the padding.

    So instead of using e.scrollHeight, you could replace it with this:
    $(this).height() + e.scrollHeight - e.clientHeight

    If height is 40, scrollHeight is 54 (46 + 8 padding) and clientHeight is 48 (40 + 8 padding), you would end up with:
    40 + 54 - 48
    which results in 40 + 6, which works out, since the scrollHeight is 6 larger than the clientHeight
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •