views:

230

answers:

2

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.

+1  A: 

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;}}());
CMS
Excellent thanks.
Mike
+1  A: 

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

K Prime