Search code examples
javascriptjquery-pluginsjquerylivechat

Send data on click - jQuery ajax chat


I have a jQuery chat with script

var name = "Guest";
    name = name.replace(/(<([^>]+)>)/ig,"");
    var chat =  new Chat();
    $(function() {
         chat.getState();
         $("#sendie").keydown(function(event) {
             var key = event.which;
             if (key >= 33) {
                 var maxLength = $(this).attr("maxlength");
                 var length = this.value.length;
                 if (length >= maxLength) {
                     event.preventDefault();
                 }
              }
              });
         $('#sendie').keyup(function(e) {
              if (e.keyCode == 13) {
                var text = $(this).val();
                var maxLength = $(this).attr("maxlength");
                var length = text.length;
                if (length <= maxLength + 1) {
                    chat.send("testtext", name);
                    $(this).val("");
                } else {
                    $(this).val(text.substring(0, maxLength));
                }
              }
         });
    });

the html structure is;

<body onload="setInterval('chat.update()', 1000)">

    <div id="page-wrap">
    
        <h2>jQuery/PHP Chat</h2>
        
        <p id="name-area"></p>
        
        <div id="chat-wrap"><div id="chat-area"></div></div>
        
        <form id="send-message-area">
            <p>Your message: </p>
            <textarea id="sendie" maxlength = '100' ></textarea>
            <button id="submit">submit</button>
        </form>
    
    </div>

</body>

This send (or save to file) message when I hit ENTER key after my message. I have replaced text area data with testtext. I want to send data when I click the submit button.

How can I do this?

I am trying to modify this example. I am trying to add a button instead of default enter key hit.


Solution

  • I solved it by removing chat send from main group and included it in trigger click function as below;

    $('#trigger').click(function() {
        chat.send("testtext", name);
        $(this).val("");
        });
    

    Thanks to @Kon, @Piotr Salaciak and @Dutchie432 for their support... :)