简体   繁体   中英

Handle 'img' click event and insert 'alt' attribute into textbox using jQuery

I'm creating a web chat page using PHP and jQuery, and I am adding an emoticon feature, which everything is done except for the jQuery portion. My problem is I'm not getting a click event when I click on the emoticon image. In the handler it is supposed to get the current message value, get the text version of the emoticon from the 'alt' attribute, and set the textbox value to its previous value + the emoticons text appended.

Here is what I tried:

(Each button appears as so: <img class="embtn" src="emoticons/happy.gif" alt=":)" /> )

$(document).ready(function(){
    // Insert Emoticon
    $(".embtn").click(function(event){
        var prevMsg = $("#usermsg").val();
        var emotiText = event.target.attr("alt");
        $("#usermsg").val(prevMsg + emotiText);
    });
});

How should I go about doing this?

The version of jQuery I am using is 1.3.

Try changing:

var emotiText = event.target.attr("alt");

to:

var emotiText = $(event.target).attr("alt");

In the future check the console for errors when you are stumped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM