Page 1 of 1

nvm I figured it out....

Posted: Mon Mar 15, 2010 11:56 am
by scarface222
ok so I have a script and a form and I just want to submit the form on enter while in the text area, but it is not working, and I am getting extremely frustrated. If anyone can spot my error, please let me know. Additionally, the form works when the send button is clicked and is submitted through a javascript function that uses $("#form").submit(function(){ to trigger the activity, I need the submit on enter to work with that restriction. Thank you in advance for any help.

Code: Select all

<script>
function enter(evt)
        {
        var charCode = (evt.which) ? evt.which : window.event.keyCode; 
         
            if (charCode == 13) 
            { 
            document.GetElementById("#form").submit();
            } 
        
        }
</script>
and a form (do not mind the escape slashes)

Code: Select all

    <form method=\"post\" id=\"form\">
        <input class=\"text user\" id=\"nick\" value=\"$session->username\" type=\"hidden\" />
        <input  class=\"text\" id=\"topic_id\" value=\"$topic_id\" type=\"hidden\" />
            
                <label>Message</label><br>
                
                <TEXTAREA onkeypress=\"enter();\" id=\"message\" maxlength=\"1000\" COLS=\"40\" ROWS=\"6\"></TEXTAREA>
        <br>
                <input id=\"send\" type=\"submit\" value=\"Send!\" />
    
    </form>

Re: quick question, submit form on enter issue

Posted: Mon Mar 15, 2010 12:01 pm
by scarface222
onkeypress=\"if (event.keyCode == 13) {this.form.submit();}\"

Ok i got it to work with this, but the other javascript form handler
$("#form").submit(function(){
for some reason does not recognize a submit from enter, only from the send button? The page just refreshes since the form has not specified action. Any idea?

Re: nvm I figured it out....

Posted: Mon Mar 15, 2010 10:31 pm
by scarface222
after much searching I found a specialized jquery function to link the enter of text area to a submit function. If someone else ever had this problem check it out.

Code: Select all

jQuery.clickOrEnter = function(element,callback) {
    jQuery(element).bind('click', function(event) {
        callback(event.target);
        event.preventDefault(); //prevent browser from following the actual href
    });
    jQuery(element).bind('keypress', function(event) {
        var code=event.charCode || event.keyCode;
        if(code && code == 13) {// if enter is pressed
            callback(event.target);
            event.preventDefault(); //prevent browser from following the actual href
        };
    });
};