Eren
Eren

Reputation: 183

Add Text to input On Click

I want to type kappa in input on image click. I have this code but it seems like it doesn't work...

document.getElementById("kappa").addEventListener('click', function () {
    var text = document.getElementById('usermsg');
    text.value = (text.innerHTML + ' kappa ');
});
<a href="#" id="kappa"><img src='http://placehold.it/50x50' width='100px'></a>
<br>
<input type="text" id="usermsg">

Where did I made a mistake? And how can I solve it?

Upvotes: 0

Views: 54

Answers (2)

mad
mad

Reputation: 1

do the following:

    document.getElementById("kappa").addEventListener('click', function(evt) {
        document.getElementById("usermsg").value = "kappa";
    });

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Use value instead of text:

document.getElementById("kappa").addEventListener('click', function () {
    var text = document.getElementById('usermsg');
    text.value = (text.value + ' kappa ');
    return false;
});

And give a return false to make it not follow the link.

Fiddle: http://codepen.io/anon/pen/QjELxJ

Upvotes: 3

Related Questions