开发者

How to create a javascript bookmarklet to paste text into all textareas on webpage?

开发者 https://www.devze.com 2022-12-18 21:10 出处:网络
I have the following code which works to a degree with text开发者_开发技巧 fields but not textareas

I have the following code which works to a degree with text开发者_开发技巧 fields but not textareas

javascript:(function(){for(var%20text=prompt('enter%20text%20to%20fill')||'',ins=document.getElementsByTagName('input'),it,m=ins.length,i=0;i<m;i++){it=ins[i];if(it.name=='text')it.value=text;};}());

Thanks for any help.


A simplified example, based on your bookmarklet:

Readable version:

(function(){
  var t = prompt('enter text to fill') || '',
  ta = document.getElementsByTagName('textarea'), n = ta.length;
  while(n--){
    ta[n].value = t;
  }
}());

Bookmarklet:

javascript:(function(){var%20t=prompt('enter%20text%20to%20fill')||'',ta=document.getElementsByTagName('textarea'),n=ta.length;while(n--){ta[n].value=t;}}());


Change:

ins=document.getElementsByTagName('input')

to

ins=document.getElementsByTagName('textarea')

and remove

if(it.name=='text')

Note: this will insert your text into all textareas in the document

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号