0

I am trying to disable a textarea from handling a keyboard event when ever a user presses the enter key of the keyboard when the textarea is empty using angularjs. I have been able to disable the submit button when the textarea is empty but I am trying to disable the textarea when from enter event when the textarea is empty.

This is my attempt:

<div class="descriptionarea">
    <textarea ng-model="trackTxt" id="input" ></textarea>
    <span class="buttonfortxtarea">
    <button ng-disabled="!trackTxt" class= "btn btn-mini description_submit" id="new-chat-button">Submit</button></span>
    </div>

When I hit enter from my textarea it triggers event using the following code

document.getElementById('input').addEventListener('keyup', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { // enter key press
            send();
        }
    });

The above code works whether the textarea is empty or not.

EDITTED:

In my app.js of angularjs

app.controller('TodoCtrl',function($scope){

    //handle javascript enter event on key up
   // function TodoCtrl($scope) {

    $scope.trackTxt;
    $scope.send = function($event){
        alert('TEST');
  }

  $scope.isEmpty = function(param){
    return param.trim().length === 0;
  }
//}

});

In jsp file

<div ng-controller="TodoCtrl">
     <textarea ng-keyup="$event.keyCode == 13 && !isEmpty(trackTxt) && send($event)" ng-model="trackTxt" id="input" ></textarea>
    <span class="buttonfortxtarea">
    <button ng-disabled="!trackTxt" ng-click="send($event)" class= "btn btn-mini description_submit" id="new-chat-button">Submit</button></span>
  </div></div>

Please how can I use angularjs to prevent the enter event from happening when ever the text-area is empty.

5
  • by enter event, do you mean form submit event? Commented Aug 18, 2016 at 15:27
  • Waht do you want to achieve by doing such disabling of event which disable some events and so ...? Commented Aug 18, 2016 at 15:30
  • Ok, may be do you want to disable submit button when there is no text in textarea haven't take in account spaces and enters? Commented Aug 18, 2016 at 15:34
  • If the textarea is blank or has no text disable it from triggering the enter keyboard using angular Commented Aug 18, 2016 at 15:37
  • I dont want the textarea to send an empty text using angularjs Commented Aug 18, 2016 at 15:40

2 Answers 2

1

You can use ng-keyup for attaching an function on enter key press,

ng-keyup="$event.keyCode == 13 && functionToCall()"

Here is demo,

Thanks

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

13 Comments

This way you can use angular directive ng-keyup for attaching key up event listener handler.
Check this update FIDDLE as this contains all the requirement you mention , Sorry i missed one on my previous one, Thanks, Hope this helps.
I am having some issues implementing this in my project
can you share how you are implementing?
which angular version you are using
|
0

You could simply check if it's empty...

document.getElementById('input').addEventListener('keyup', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && $(this).val() != "") { // enter key press
            send();
        }
});

This also may work:

document.getElementById('input').addEventListener('keyup', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && $(this).val().length <= 0) { // enter key press
            send();
        }
});

To prevent the default enter use preventDefault() Event Method

document.getElementById('input').addEventListener('keyup', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && $(this).val().length <= 0) { // enter key press
            e.preventDefault(); //This avoid the enter...
        }
});

How about the sendChat()...

document.getElementById('input').addEventListener('keyup', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && $(this).val().length <= 0) { // enter key press
            e.preventDefault(); //This avoid the enter...
        }
        else if(code == 13 && $(this).val().length > 0) {
            sendChat();
        }
});

8 Comments

This prevents enter event of the keyboard
On hitting the enter keyboard, nothing happens even when there is text in the textarea
I want the enter key of the keyboard to happen only when there is text in the textarea
@user6731422 document.getElementById('input').addEventListener('keyup', function (e) { var code = (e.keyCode ? e.keyCode : e.which); if (code == 13 && $(this).val().length > 0) { e.preventDefault(); } }); this one (the last one, should be doing exactly that...
what about this method sendChat();
|

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.