0
    $("#sendBtn").click(function(event){
        console.log("sendMsg  called")
        if (websocket == undefined || websocket == null) {
            //alert('WebSocket connection not established, please connect.');
            return;
        }
        var msg = $("#msg").val();
        console.log(msg);
        if (msg == "") {
            return;
        }
        else {
            var data = {};
            data["sender_id"] = current_user_id;
            data["sender_name"] = current_user_name;
            data["receiver_id"] = current_receiver_id;
            data["receiver_name"] = current_receiver_name;
            data["text"] = msg;

            $('.contact.active .preview')[0].innerHTML = '<p class="leftNewMsg"></p>';
            $('.contact.active .preview .leftNewMsg')[0].textContent = msg;

            websocket.send(JSON.stringify(data));
            $("#msg").val("");
            $('<li class="sent"> <img src="'+ current_user_image +'" alt="" /><p class="newMsg"></p></li>').appendTo($('.messages ul'));
            var nodes = document.getElementsByClassName("newMsg");
            nodes[nodes.length-1].textContent = msg;

            $('.message-input input').val(null);

            scrollToBottom();
            return;
        }
        event.preventDefault();
    })
    function scrollToBottom() {
        var div = document.getElementById('messages_div');
        div.scrollTop = div.scrollHeight;
        return;
    }

here is scrollToBottom function

<button  id="sendBtn" class="submit"><i class="fa fa-paper-plane"></i>

enter image description here

here is my code and my problem is when the botton be clicked the js will repeatly run the same function as below but i dont know why, could somebody help me? please

I fix the problem, its because i import unknown .js file in my header, after commented it, everything is ok

hongyun
  • 35
  • 1
  • 7
  • Can you paste the function body of scrollToBottom();. What is it doing? – intekhab Jun 03 '20 at 08:36
  • It seems like you somewhere all `$("#sendBtn").click()` again and again, so there is more than one click event binded to button – Justinas Jun 03 '20 at 08:38
  • $._data( $("#sendBtn")[0], "events" );. Check using this code, how many click events are bind on "#sendBtn" https://stackoverflow.com/questions/2008592/can-i-find-events-bound-on-an-element-with-jquery – intekhab Jun 03 '20 at 08:40
  • try using console.trace, console.warn or console.error to see the stack trace, instead of console.log – ovikoomikko Jun 03 '20 at 09:21

0 Answers0