-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_changeText.html
94 lines (73 loc) · 2.75 KB
/
js_changeText.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="ja" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>js_test</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" lang="ja" xml:lang="ja" content="">
<meta name="keywords" lang="ja" xml:lang="ja" content="">
<link href="https://necolas.github.io/normalize.css/3.0.2/normalize.css" rel="stylesheet">
<style>
</style>
<script>
//Tips
//In japascript, it is used as global variable when it doesn't use var .. like this
global = "global test";
let glocal = "local test";
//Name space ... Need to study later..
var NS = {};
NS.msg = "nama space";
NS.arr = {test:"TEST"};
function click_fnc(get_id){
console.log(global);
//console.log(local); This gets an error because it is not defined
console.log(NS.msg);
console.log(NS.arr);
//The element that I click
let select_id = get_id; //All element
console.log(select_id);
let select_id2 = get_id.id; //Id name that I click
console.log(select_id2);
//Associative array
let fruit_arr = { apple:"りんご", banana:"バナナ", melon:"メロン", grape:"グレープ"}
// console.log(fruit_arr);
//How to get a value from this associative array
// console.log(fruit_arr.apple); //りんご
// console.log(fruit_arr['apple']); //りんご
//Get the value of this id
// var fruit = document.getElementById('fruit_id').innerText;
// console.log(fruit); //りんご
var fruit = document.getElementById(select_id2).innerText;
console.log(fruit);
//Checl the key if it exists
if(fruit in fruit_arr) { //Get the value from the key
// console.log("val");
document.getElementById(select_id2).innerText = fruit_arr[fruit];
}else{ //Want to get the key from the value..
// console.log("key");
/* didn't work
let filter_val = fruit_arr.filter(function(fruit) {
return index;
});
*/
let filter_val = Object.keys(fruit_arr).filter( (key) => {
return fruit_arr[key] === fruit;
});
// console.log(filter_val); //array
// console.log(filter_val[0]);
let fruit_en = filter_val[0];
document.getElementById(select_id2).innerText = fruit_en;
}
//Chenge the value of this id
//document.getElementById("fruit_id").innerText = 'te';
// document.getElementById("fruit_id").innerText = (fruit_arr.apple);
// document.getElementById("fruit_id").innerText = fruit_arr[fruit];
// document.getElementById(select_id2).innerText = fruit_arr[fruit];
}
</script>
</head>
<body>
<p id="fruit_id" onclick="click_fnc(fruit_id);">apple</p>
<p id="fruit_id2" onclick="click_fnc(fruit_id2);">banana</p>
<body>
</html>