使用javascript将文本插入textarea中以获取Google Chrome扩展名 [英] Inserting text into textarea with javascript for google chrome extension

查看:102
本文介绍了使用javascript将文本插入textarea中以获取Google Chrome扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展现有的Google chrome扩展程序的功能.使用Wrike google chrome扩展程序,我的目标是添加一个(或多个)按钮,该按钮会将一些文本添加到描述字段(文本区域).理想的效果是,如果用户单击添加模板"按钮,则代码/文本将插入到Wrike chrome扩展程序固有的id ="description"的文本区域中.在下面,您将找到我一直在使用的一些代码.

I am trying to extend the functionality of an existing google chrome extension. Using the Wrike google chrome extension, my goal is to add a button (or buttons) which will add some text to the description field (a textarea). The desired effect will be that if a user clicks an "Add Template" button, the code/text will be inserted into the textarea with id="description" which is native to the Wrike chrome extension. Below you will find some of the code that I have been working with.

这是表格的描述部分.位于createTask.html

Here is the description part of the form. Located in createTask.html

<div class="main-description main-row">
    <textarea placeholder="Click to add description" id="description" class="main-description-text"></textarea>
</div>

这是我创建的按钮,它将开始将文本添加到文本区域:

This is the button that I created which will initiate adding the text to the textarea:

<span class="btn m-green btn-addtemp" id="link">Add Template</span>

在template.js中(已正确链接为createTask.html中的外部.js文件:

Within template.js (which is properly linked to as an external .js file within createTask.html:

function insertText(text) {
    document.getElementById("description").innerHTML = text;
}

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
    link.addEventListener('click', function() {
        insertText('Sample text here');
    });
});

我可以获取此代码以将此处的示例文本"插入具有ID的div中,但是无法将其插入具有id = description的文本区域中.任何帮助将不胜感激,如有必要,我非常乐意提供更详细的信息.谢谢!

I can get this code to insert the 'Sample text here' into a div with an ID, but cannot get it to insert into the textarea with id=description. Any help would be greatly appreciated and I am more than happy to provide more detailed information if necessary. Thanks!

推荐答案

尝试使用element.value:

Try with element.value:

function insertText(text) {
    document.getElementById("description").value= text;
}

这篇关于使用javascript将文本插入textarea中以获取Google Chrome扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆