javascript - jquery click does not work -
anyone have idea why jquery click won't work?
it's attached hyperlink.
jquery(function ($) { $(".delete").click(function(e) { alert("hello"); }); var socket = io.connect(); var $messageform = $('#sendmessage'); var $messagetitle = $('#title'); var $messagebox = $('#message'); var $chat = $('#chat'); $messageform.click(function (e) { if ($.trim($("#title").val()).length === 0) { alert('you must provide valid input'); $messagetitle.val(''); $messagebox.val(''); return false; } if ($.trim($("#message").val()).length === 0) { alert('you must provide valid input'); $messagetitle.val(''); $messagebox.val(''); return false; } else { e.preventdefault(); socket.emit('send message', '<b>' + $messagetitle.val() + '</b>' + ' - ' + $messagebox.val() + ' ' + '[' + '<a class="delete" href="#">delete</a>' + ']'); $messagetitle.val(''); $messagebox.val(''); } }); socket.on('new message', function (data) { $chat.prepend(data + "<br/>"); }); });
since delete links dynamically generated, need use event delegation:
$('#chat').on('click', '.delete', function(e) { alert("hello"); });
Comments
Post a Comment