Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1030898 // hjs_autoExpandingTextAreas

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5384L/31K).

// needs JQuery
// might actually handle divs too in addition to textareas
sS hjs_autoExpandingTextAreas(bool verbose default false) {
  ret
    hcss([[
      .auto-expand {
        max-height: 30em; vertical-align: top;
      }
    ]])
  + hjs(replaceDollarVars([[
    var autoExpandVerbose = $verbose;
    
    var autoExpandTextArea = function (ta) {
      ta.style.height = "1px";
      
      // Get the computed styles for the element
      var computed = window.getComputedStyle(ta);
      
      var h = ta.scrollHeight
        + parseInt(computed.getPropertyValue('border-top-width'))
        + parseInt(computed.getPropertyValue('border-bottom-width'));
    
      if (autoExpandVerbose)
        console.log("Calculated auto-expand height " + h + " for " + ta);
      ta.style.height = h + 'px';
    };
  
    document.addEventListener('input', function (event) {
      if (!event.target.classList.contains("auto-expand")) return;
      autoExpandTextArea(event.target);
    }, false);
  
    //$(document).ready(function() {
    document.addEventListener("DOMContentLoaded", function(event) { 
      $(".auto-expand").each(function() { autoExpandTextArea(this); });
    });
  ]], +verbose);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030898
Snippet name: hjs_autoExpandingTextAreas
Eternal ID of this version: #1030898/20
Text MD5: f27214f18c6563aec0aef543d3fde2ea
Transpilation MD5: 77a36ebaba408bb238f008309bed75db
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-16 00:25:39
Source code size: 1240 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 261 / 411
Version history: 19 change(s)
Referenced in: [show references]