简体   繁体   中英

Greasemonkey (JavaScript) textarea question

I have written a basic Greasemonkey script which passes an output out to a textarea , like this:

var obj = document.getElementById("comment").innerHTML = "\n" + "Note:" + "\n" + "\n" + output_string;

It works like a charm, if you change the value from the source, it will update the textarea. However, as soon as you write anything yourself inside the textarea and select a value, it will not overwrite what you written inside the textarea. And you need to refresh the page entirely to be able to use the function again. Why is that?

The textarea 's value attribute is set as soon as you type something into it. This overrides any innerHTML value.

You should be using the value attribute to set the contents of input elements like textarea .

Try this instead:

var obj = document.getElementById("comment").value = "\n" + "Note:" + "\n" + "\n" + output_string;

If you need your text area to always display the same text I suggest you attach blur event on it using your customized greasemonkey script. Blur event would do the same thing as it does now. The only change would be that textarea will be updated the moment cursor focuses off the textarea .

If that's what you were after, 'cause I didn't understand it quite clearly what are you trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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