Fill and submit textarea programmatically in javascript

后端 未结 1 1935
梦如初夏
梦如初夏 2021-01-06 14:57

I\'m trying to automate the submission of replies to my Steemit posts using plain JS or Jquery.

I used the javascript code below but the button remains disabled and

1条回答
  •  清酒与你
    2021-01-06 15:36

    After trying everything, I found out that the problem seems to be a bug in React on triggering onchange for textareas.

    More info on the bug

    There is a workaround..

    Solution:

    function setNativeValue(element, value) {
      const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
      const prototype = Object.getPrototypeOf(element);
      const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
    
      if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(element, value);
      } else {
        valueSetter.call(element, value);
      }
    }
    
    var textarea = document.getElementsByTagName('textarea')[0];
    setNativeValue(textarea, 'My automated comment here');
    textarea.dispatchEvent(new Event('input', { bubbles: true }));
    

    0 讨论(0)
提交回复
热议问题