Question

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');
        }
    }
});
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top