1

I am trying to add the auto resize feature on 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. Therefore, it cannot be resize. Is there anyway I can trigger keyup or other event to trigger function to get scrollHeight programatically??

autoResizeTextArea = (e) => {
  const element = e.target
  element.style.height = 'auto'
  element.style.overflowY = 'hidden'
  element.style.height = element.scrollHeight + 'px'
}
fiddlest
  • 1,262
  • 2
  • 17
  • 42
  • 1
    previously answered question https://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript – zfrisch Jul 18 '17 at 17:29
  • I am getting Unhandled rejection InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is uninitialized. Do I have to add event with addEventListener? I am currently adding event with react (onKeyUp ) – fiddlest Jul 18 '17 at 18:37

2 Answers2

0

If you control the javascript library that changes the code you can simply trigger the event manualy by calling element.onchange()

UPDATE

and dont forget to attach the event listenner onchange to your element

Please note it should be set via js element.onchange insted of via html directive.

here is a working example jsfiddle

HTML:

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

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; 
Ivan Vilanculo
  • 648
  • 1
  • 11
  • 25
0

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

zfrisch
  • 8,474
  • 1
  • 22
  • 34