Table for keyCodes value

Table of Values

Below is the table for keycode value when using KeyboardEvent.

Key Code Key Code Key Code
backspace 8 tab 9 enter 13
shift 16 ctrl 17 alt 18
pause/break 19 caps lock 20 escape 27
(space) 32 page up 33 page down 34
end 35 home 36 left arrow 37
up arrow 38 right arrow 39 down arrow 40
insert 45 delete 46 0 48
1 49 2 50 3 51
4 52 5 53 6 54
7 55 8 56 9 57
a 65 b 66 c 67
d 68 e 69 f 70
g 71 h 72 i 73
j 74 k 75 l 76
m 77 n 78 o 79
p 80 q 81 r 82
s 83 t 84 u 85
v 86 w 87 x 88
y 89 z 90 left window key 91
right window key 92 select key 93 numpad 0 96
numpad 1 97 numpad 2 98 numpad 3 99
numpad 4 100 numpad 5 101 numpad 6 102
numpad 7 103 numpad 8 104 numpad 9 105
multiply 106 add 107 subtract 109
decimal point 110 divide 111 f1 112
f2 113 f3 114 f4 115
f5 116 f6 117 f7 118
f8 119 f9 120 f10 121
f11 122 f12 123 num lock 144
scroll lock 145 semi-colon 186 equal sign 187
comma 188 dash 189 period 190
forward slash 191 grave accent 192 open bracket 219
back slash 220 close braket 221 single quote 222

Live Demo

You can press any key, and get that key’s value in this live demo.

See the Pen event.keyCode tester by Chris Coyier (@chriscoyier) on CodePen.

Reference


This is the end of post

How to remove a key from an object in Javascript?

Question

I have an object below:

1
2
3
4
5
var thisIsObject= {
'Cow' : 'Moo',
'Cat' : 'Meow',
'Dog' : 'Bark'
};

I want to remove the key Dog and its value. What should I do?

Answer

There are three ways to remove a key from an object.

1
2
3
4
5
6
7
8
9
// Example 1
var key = "Dog";
delete thisIsObject[key];

// Example 2
delete thisIsObject["Dog"];

// Example 3
delete thisIsObject.Dog;

Reference


This is the end of post

How to push an array into an array inside an object in Javascript?

Question

There is an object like below:

1
2
3
4
var myObj = {
arrayOne: [],
arrayTwo: []
};

And one array is var newArray = ['A', 'B'];

I want to add newArray into arrayOne: [], the result should be like below

1
2
3
4
{ 
arrayOne: [['A', 'B']],
arrayTwo: []
};

What should I do?

Answer

There are two ways to add newArray inside arrayOne of myObj.

  1. obj.arrayOne.push(newArray);
  2. obj['arrayOne'].push(newArray);

Reference


This is the end of post

How to change background color upon hovering a div?

Question

There is a div as below.

1
2
<div class="e">Wei Xia</div>
<a href="http://weixia.info/">weixia.info</a>

How to change the background color when I hover those two element?

Answer

1
2
3
4
5
6
7
.e:hover {
background-color: #ff0000;
}

a:hover {
background-color: yellow;
}

By using :hover will update the background color. You can view the demo below.

Live Demo

Reference


This is the end of post

如何理解 Javascript 中的 For 循环?

Javascript 里的 For 循环有很多种,接下来我们一一来解析一遍。

For Loop

这是最基础的用法,用 For loop 来遍历一个数组,如下所示:

1
2
3
for (var index = 0; index < myArray.length; index++) {
console.log(myArray[index]);
}

forEach - ES 5

forEach 是 ES 5 里的一个用法,如下所示:

1
2
3
myArray.forEach(function (value) {
console.log(value);
});

for…in - ES 5

for...in 循环实际是为循环可枚举 (enumerable) 对象而设计的,如下所示:

1
2
3
4
5
6
7
8
9
10
var obj = {a:1, b:2, c:3};

for (var prop in obj) {
console.log("obj." + prop + " = " + obj[prop]);
}

// 输出:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

for…of - ES 6

for ... of 是 ES 6 里的新用法,语法如下:

1
2
3
for (var value of myArray) {
console.log(value);
}

for...of 的语法看起来跟 for...in 很相似,但它的功能却丰富的多,它能循环很多东西。

循环一个数组 Array

1
2
3
4
5
6
7
8
let iterable = [10, 20, 30];

for (let value of iterable) {
console.log(value);
}
// 10
// 20
// 30

循环一个字符串 String

1
2
3
4
5
6
7
8
let iterable = "boo";

for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"

循环一个类型化的数组 TypedArray

1
2
3
4
5
6
7
let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {
console.log(value);
}
// 0
// 255

循环一个 Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3

for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]

循环一个 Set

1
2
3
4
5
6
7
8
let iterable = new Set([1, 1, 2, 2, 3, 3]);

for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3

循环一个 DOM 集合

1
2
3
4
5
6
7
// Note: This will only work in platforms that have
// implemented NodeList.prototype[Symbol.iterator]
let articleParagraphs = document.querySelectorAll("article > p");

for (let paragraph of articleParagraphs) {
paragraph.classList.add("read");
}

循环一个生成器 Generators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function* fibonacci() { // a generator function
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}

for (let n of fibonacci()) {
console.log(n);
// truncate the sequence at 1000
if (n >= 1000) {
break;
}
}

Reference


This is the end of post

What is the order for tabindex in HTML?

Question

Like title says, what is the order for the tabindex attribute in html?

Answer

Code example:

1
2
3
4
5
6
7
<input tabindex="3">
<input tabindex="0">
<input tabindex="-1">
<input>
<input tabindex="2">
<input tabindex="1">
<span tabindex="4"> This wouldn't normally receive focus</span>

Order:

(3) Will receive focus third
(0) In normal source order
(-1) Will not receive focus
(n/a) In normal source order
(2) Will receive focus second
(1) Will receive focus first

<span tabindex="4">This wouldn't normally receive focus</span> has (4) largest value. Next tab will give focus to the rest of tabnavigable elements in code source order.

Reference


This is the end of post

How to add accessibility label in a tag in HTML?

Question

My code is below:

1
<li id="id_name" class="class_names"><a href="http://linktothepage.com/page" aria-label="TITLE OF MY PAGE">TITLE OF MY PAGE</a></li>

I added aria-label in the <a> tag, but it doesn’t work with screen reader software.

What should I do?

Answer

aria-label sometimes doesn’t work with JAWS in Windows PC. Need to use title in <a> tag instead.

The solution is:

1
<li id="id_name" class="class_names"><a href="http://linktothepage.com/page" title="TITLE OF MY PAGE">TITLE OF MY PAGE</a></li>

Reference


This is the end of post