0

I'm trying to create a text input using key navigation. With left and right arrows you choose position, on enter you activate the current letter and then you can choose letter with up and down keys.

What I want to achieve is a delete function, so for example, if I choose the < sign and press enter the current letter should be deleted or at least not visible. Is this even possible to do without using more keys?

This is my code:

var app = angular.module('ccApp',[]);

    var letters = [
                    '<','a','b','c','d','e','f','g','h','i','j','k','l','m','n'
                ];

    app.controller("ccInputController",function($scope, $http) {

        $('li:first').focus();

            $('li').on('keydown', function(e){

                    e.preventDefault();

                    var keyCode = e.which;
                        key = {up: 38, down: 40, right: 39, left: 37, enter: 13};

                    letterIndex = letters.indexOf($(this).html());

                    switch(e.which) {
                        case key.up:
                        if ($('li').hasClass('active')) {
                            letterIndex = letterIndex + 1;
                                $(this).html(letters[letterIndex]);
                        }

                        break;

                        case key.down:
                            if ($('li').hasClass('active')) {
                            letterIndex = letterIndex - 1;
                                $(this).html(letters[letterIndex]);
                        }
                        break;

                        case key.right:
                            // check if li is not active, then move right
                            if (!$('li').hasClass('active')) {
                                $('li:focus').closest('li').next().focus();
                            }
                        break;

                        case key.left:
                            // check if li is not active, then move left
                            if (!$('li').hasClass('active')) {
                                $('li:focus').closest('li').prev().focus();
                            }
                        break;

                        case key.enter:
                            $(this).closest('li').toggleClass('active');
                        break;

                    }

            });
        });

And the HTML:

<ul>
    <li tabindex="0">i</li>
    <li tabindex="0">n</li>
    <li tabindex="0">p</li>
    <li tabindex="0">u</li>
    <li tabindex="0">t</li>
</ul>

Fiddle

3
  • put your code in jsfiddle Commented May 13, 2015 at 7:53
  • 1
    I'm still a little unclear about your goals... what exactly is supposed to happen when you hit enter? Can you just do something like $(this).closest('li').remove()? Commented May 13, 2015 at 8:16
  • The problem is enter is toggle the active state, so when you press enter on a letter that letter is activated and you can choose it. But I want the enter key also act as remove but only if you choose the < symbol. Commented May 13, 2015 at 8:21

1 Answer 1

1

To remove any element using jquery, simply call remove(). In your case, on the enter handler, after validating the value matches, use $(this).remove().

Also, you need to use .text() instead of .html() because the < gets transformed to &lt; in html.

Corrected fiddle

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

1 Comment

Oh, thank you so much!! I felt like I was pretty close but didn't think of the 'if(letterIndex == 0...'. That made it!

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.