Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent autosize manually? #272

Closed
gregblass opened this issue Nov 20, 2015 · 9 comments
Closed

Prevent autosize manually? #272

gregblass opened this issue Nov 20, 2015 · 9 comments

Comments

@gregblass
Copy link

Is it possible to manually prevent autosize from triggering?

I would do this in an if statement for certain cases (for example, I'm submitting the form on the enter key, and I don't want it to resize the box in that case...or display a modal saying 'you didn't enter anything' if the textarea is blank). Both cases I don't want autosize to fire.

@jackmoore
Copy link
Owner

Have you attempted this already? I don't see why either of those things would cause autosize to trigger.

@gregblass
Copy link
Author

...not following. Just press enter on an autosize. It will increase the size of the box...

@gregblass
Copy link
Author

Maybe the confusion stems from the fact that I'm using a remote form? There is no page refresh. So the textarea remains larger due to the enter key press.

@jackmoore
Copy link
Owner

I'm going to say no, because this doesn't seem like something the script should have to account for. Forms aren't normally submitted when pressing enter, if a textarea is receiving input.

@gregblass
Copy link
Author

I respectfully disagree... Check out facebook on a desktop. Type a comment and press enter. The form is submitted.

@gregblass
Copy link
Author

Regardless of our opinions on how apps should function, I'm simply asking if there is a way to "preventDefault", so to speak, in certain cases. Simple question, seems like the answer is no. If not I can take a look at the source code and see if I can figure out the easiest way to implement something like that.

@jackmoore
Copy link
Owner

That's not browser default behavior for submitting forms, it's not really a matter of opinion. But, I see what you are getting at. You could bind to the keydown event to prevent the default action, which should prevent textarea from receiving an input event (this is what autosize is listening to). For example:

var ta = document.querySelector('#my-textarea');
var form = document.querySelector('#my-form');

ta.addEventListener('keypress', function(e){
    if (e.keyCode === 13) {
        e.preventDefault(); 
        form.submit();
    }
});

autosize(ta);

@gregblass
Copy link
Author

Excellent, thanks for the help. I was doing e.preventDefault, but hooking into the wrong place, so it wasn't actually preventing autosize from firing. I bet this will do the trick. Cheers!

@gregblass
Copy link
Author

I actually had it pretty close in the first place, it was only a matter of changing 'keyup' to 'keypress'. Thanks for helping me track that down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants