Reputation: 470
I have a bookmarklet,
What I am trying to do is, appending an element to current focused input.For example, if user focused (clicked) on textarea, and click my bookmarklet, I want to add "Hello" into that textarea. I have tried this;
<a href=" javascript:(function(){
var theDiv = document.getElementById("contentarea");
var content = document.createTextNode("Hello");
theDiv.appendChild(content);
})();
">this</a>
But it only works for the div id (contentarea) that I have included. How to append on current focused input ?
Upvotes: 1
Views: 156
Reputation: 6120
Try with document.activeElement
, like:
<a href='javascript:(function(){
var theDiv = document.activeElement;
var content = document.createTextNode("Hello");
theDiv.appendChild(content);
})();
'>this</a>
Upvotes: 3
Reputation: 59252
Escape the quotes and use document.activeElement
<a href='javascript:(function(){
var theDiv = document.activeElement;
var content = document.createTextNode("Hello");
theDiv.appendChild(content);
})();
'>this</a>
Upvotes: 1