-1

I have an HTML table with input text in every td. I'm trying to create a function that will allow arrow keys to navigate my td's.

I want to highlight the value of the selected cell when every arrow key is clicked, just like how tab works. Does anyone here have an idea how to achieve this? Thank you.

Code Here

5
  • We're expecting you to respect the given guidance you get during asking. Commented Nov 7, 2016 at 6:42
  • Add to your css: input:focus { background-color: <your color goes here>; } Looks as though support for this is pretty wide spread. developer.mozilla.org/en/docs/Web/CSS/:focus Commented Nov 7, 2016 at 6:42
  • @jeff No. I want the text to be highlighted just like when you hit tab. Commented Nov 7, 2016 at 6:44
  • Change background-color to color doh. Commented Nov 7, 2016 at 6:47
  • please see sample link that I posted. Thanks for the help anyway. What I'm trying to do is when I click arrow key that cell witch compose of input text will automatically select that value, just like the function of the tab key. Commented Nov 7, 2016 at 6:49

2 Answers 2

1

You can find input separately, and add a class to Active input in function rePosition() as,

$('#navigate tr td').find('input').removeClass('yourClassforText');
$('#navigate tr td').eq(active).find('input').addClass('yourClassforText');

// to select text inside input
$('#navigate tr td').eq(active).find('input').select();

old Demo Here

Update

As mentioned another problem by Op, is to remove only selected text.
You need to add a check on a keydown event as,

var inp = String.fromCharCode(event.keyCode);
if (!(/[a-zA-Z0-9-_ ]/.test(inp) || event.keyCode == 96)){
    //if user is entering some text, neglect rePosition & reCalculate events
}

Updated Demo

Update - 2:

Demo with thead

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

13 Comments

but the text value is not selected/highlighted. I want it to function the way tab key works.
@x'tian You can achieve that using select() in Jquery. answer updated
Wow. Just what I'm looking for.Thank you! But why it is only allowed with one character only? Every time i try to input a word it keeps on adding just one letter only.
@x'tian, you have written code on keydown event, so its clearing previous value. Need to assign it explicitly. by the way, it could be different Question.. the answer is updated!
Oh gosh sorry. Another error encountered tried to resolve it before asking to you but didn't know how. Why is that everytime I enter 0(zero) it clears.
|
1

You can get value. Use this code in re-position function:

alert($('#navigate tr td').eq(active).find('input').val());

3 Comments

But how can I do it without the alert? I mean I just wanted to select/highlight the text so that when I enter new value it will be replaced. Thank you.
You're welcome. That is not for one character only. It shows value when re-position function works. That's why, you saw the existing value, not new value. Try more than one on same cell. you will see.
Oh gosh sorry. Another error encountered tried to resolve it before asking to you but didn't know how. Why is that everytime I enter 0(zero) it clears.

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.