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).

1  
// needs JQuery
2  
// might actually handle divs too in addition to textareas
3  
sS hjs_autoExpandingTextAreas(bool verbose default false) {
4  
  ret
5  
    hcss([[
6  
      .auto-expand {
7  
        max-height: 30em; vertical-align: top;
8  
      }
9  
    ]])
10  
  + hjs(replaceDollarVars([[
11  
    var autoExpandVerbose = $verbose;
12  
    
13  
    var autoExpandTextArea = function (ta) {
14  
      ta.style.height = "1px";
15  
      
16  
      // Get the computed styles for the element
17  
      var computed = window.getComputedStyle(ta);
18  
      
19  
      var h = ta.scrollHeight
20  
        + parseInt(computed.getPropertyValue('border-top-width'))
21  
        + parseInt(computed.getPropertyValue('border-bottom-width'));
22  
    
23  
      if (autoExpandVerbose)
24  
        console.log("Calculated auto-expand height " + h + " for " + ta);
25  
      ta.style.height = h + 'px';
26  
    };
27  
  
28  
    document.addEventListener('input', function (event) {
29  
      if (!event.target.classList.contains("auto-expand")) return;
30  
      autoExpandTextArea(event.target);
31  
    }, false);
32  
  
33  
    //$(document).ready(function() {
34  
    document.addEventListener("DOMContentLoaded", function(event) { 
35  
      $(".auto-expand").each(function() { autoExpandTextArea(this); });
36  
    });
37  
  ]], +verbose);
38  
}

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: 186 / 316
Version history: 19 change(s)
Referenced in: [show references]