user3304007
user3304007

Reputation: 470

Append on current focused input?

I have a bookmarklet,

enter image description here

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

Answers (2)

dkasipovic
dkasipovic

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

Amit Joki
Amit Joki

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

Related Questions