简体   繁体   English

通过api调用在值更改时自动调整文本区域的大小(纯JavaScript)

[英]Auto resize text area on value change via api call ( pure javascript )

I am trying to add the auto resize feature on textarea. 我正在尝试在textarea上添加自动调整大小功能。 The code below works well only if I type something. 仅当我键入某些内容时,下面的代码才能很好地工作。 However, if I just updates value which is from api, it does not trigger any event. 但是,如果我只是更新api中的值,则不会触发任何事件。 Therefore, it cannot be resize. 因此,无法调整大小。 Is there anyway I can trigger keyup or other event to trigger function to get scrollHeight programatically?? 无论如何,我可以触发键盘输入或其他事件来触发函数以编程方式获取scrollHeight吗?

autoResizeTextArea = (e) => {
  const element = e.target
  element.style.height = 'auto'
  element.style.overflowY = 'hidden'
  element.style.height = element.scrollHeight + 'px'
}

If you control the javascript library that changes the code you can simply trigger the event manualy by calling element.onchange() 如果您控制更改代码的JavaScript库,则只需调用element.onchange()即可手动触发事件element.onchange()

UPDATE UPDATE

and dont forget to attach the event listenner onchange to your element 并且不要忘记将事件侦听器onchange附加到您的元素

Please note it should be set via js element.onchange insted of via html directive. 请注意,应通过html指令通过js element.onchange进行设置。

here is a working example jsfiddle 这是一个工作示例jsfiddle

HTML: HTML:

<textarea id="mytext"></textarea>
<button id="mybtn">change input</button>

JS: JS:

var onChange = function(){
    alert('input changed');
}

var changeInput = function(){
  text.value = 'Hello World';
    text.onchange();
}

button = document.getElementById('mybtn');
button.onclick = changeInput;

text = document.getElementById('mytext');
text.onchange = onChange; 

https://jsfiddle.net/ab9hkbL7/1/ https://jsfiddle.net/ab9hkbL7/1/

let textarea = document.querySelector("#myTextArea");
let tael = textarea.addEventListener.bind(textarea);

let autoResizeTextArea = (e) => {
  const element = e.target
  element.style.height = 'auto'
  element.style.overflowY = 'hidden'
  element.style.height = element.scrollHeight + 'px'
}

tael('update', autoResizeTextArea);
tael('keyup', autoResizeTextArea);
//add event listeners for update and keyup

//for demonstration set value big enough that it will resize box:
textarea.value = `lorem Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;

//setup promise to resolve after 2 seconds. 
//This will simulate fetching external data.

let newPromise = new Promise((res, rej) => {
  setTimeout(() => {
    res('resolved');
  }, 2000)
}).then((success) => {

//create the update event and dispatch it
  let myEvent = new CustomEvent("update");
  textarea.dispatchEvent(myEvent);
});

for more information please take a look at MDN: dispatchEvent CustomEvent 有关更多信息,请查看MDN: dispatchEvent CustomEvent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM