0

I wrote a script that trigger a submit button when i press enter key in a textarea, the TextArea returns to the line before the submit is activated.

I want to obstruct the TextArea to not return to the line

there is my script

jQuery( "#div1" ).on( "keydown", function(event) {
    if(event.which == 13){ 
        if(document.getElementById('checkbox').checked){             
            jQuery("#envoyer").trigger('click');
        }
    }
});

1 Answer 1

1

You need to cancel the event!

event.preventDefault();

Just make sure to only do that when you're submitting the form - it'd be very easy to accidentally nuke all ability to type in the textarea at all ;)

Also, as a usability hint, I would suggest adding && !event.ctrlKey && !event.shiftKey to your if condition, as this allows people to enter line breaks manually as they would in many chat programs - if you explicitly don't want newlines, you may be better just using <input type="text" /> instead of <textarea>, especially since this comes with built-in Enter-to-submit-form functionality.

Sign up to request clarification or add additional context in comments.

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.