Svetoslav
Svetoslav

Reputation: 4686

jquery append problem

I have an input (at chat) and emoticons. I need to set when some body click on emoticon, to add its code to the input(without deleting the current text) For example I have written: "Hello " I press the smile image and the input must become "Hello :):" !!! NOT SUBMITED still at the input field!

$("img.emots").click(function() { var emots=$(this).attr("title"); 
    $("#message_text").append(emots); });

I use this but its not working (its putting the emoticon between input tags <input>HERE</input>

What I need to do ??? :(

Upvotes: 0

Views: 220

Answers (3)

rickyduck
rickyduck

Reputation: 4084

<input>HERE</input> is not valid markup. Try <textarea>HERE</textarea> and then

$("img.emots").click(function() { 
    var emots=$(this).attr("title"); 
    $("textarea").val($("textarea").val()+emots); 
});

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817138

.append is for appending child nodes to a node. An input element does not have any children. You have to change its value, using .val() [docs]:

$("#message_text").val(function(i, value){
    return value + emots;
});

Upvotes: 1

Samich
Samich

Reputation: 30175

$("img.emots").click(function() { 
    var emots= $(this).attr("title"); 
    var $txt = $("#message_text");
    $txt.val($txt.val() + " " + emots); 
});

Upvotes: 1

Related Questions