1

I have this piece of code

// watch textarea for release of key press
        $('#sendie').keyup(function(e) {
            if (e.keyCode == 13) {
                var text = $(this).val();
                var maxLength = $(this).attr("maxlength");
                var length = text.length;
                // send 
                if (length <= maxLength + 1) {
                    chat.send(text, name);
                    $(this).val("");
                } else {
                    $(this).val(text.substring(0, maxLength));
                }
            }
        });

What it does it that it sends a message typed in a textarea to a certain email address once someone hits the enter button. How can i change this to use a submit button as in the code below?

< div id="page-wrap">
        < form id="send-message-area">
            < p>Your message: </p>
            < textarea id="sendie" maxlength = '100' placeholder ="Send Your Message">  < /textarea>
    < input type="submit">
        < /form>

    < /div>

2 Answers 2

2

So attach the code to the submission of the form

$("#send-message-area").on("submit", function (e) {
    var elem = $("#sendie");
    var text = elem.val();
    var maxLength = parseInt(elem.attr("maxlength"),10);
    var length = text.length;
    // send 
    if (length <= maxLength + 1) {
            chat.send(text, name);
            elem.val("");
    } else {
            elem.val(text.substring(0, maxLength));
    }
    e.preventDefault();
});
Sign up to request clarification or add additional context in comments.

Comments

1
$("#send-message-area").on("submit", function (e) {
var text = $("#sendie").val();
var maxLength = $("#sendie").attr("maxlength");
var length = text.length;
// send 
if (length <= maxLength + 1) {
        chat.send(text, name);
    alert(text);
        $("#sendie").val("");
} else {
        $("#sendie").val(text.substring(0, maxLength));
}
e.preventDefault();
});

check sample here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.