What I am doing wrong?
javascript:document.getElementsByTagName('textarea').innerHTML='inserted';
I want to create a bookmarklet to insert simple text to a textarea on a given webpage.
Use the value
property rather than innerHTML
and make sure your code evaluates to undefined
, which you can do by wrapping it in a function with no return
statement. If you don't do this, the contents of the page will be replaced with whatever your code evaluates to (in this case, the string 'inserted').
javascript:(function() {document.getElementsByTagName('textarea')[0].value = 'inserted';})();
Update 14 January 2012
I failed to spot the fact that the original code was treating document.getElementsByTagName('textarea')
as a single element rather than the NodeList
it is, so I've updated my code with [0]
. @streetpc's answer explains this in more detail.