-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
156 lines (135 loc) · 4.34 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS assignment</title>
<style>
body{
margin-left: 40%;
}
tr{
cursor: pointer;
}
.selected{
background-color:rgb(104, 104, 104);
color: #fff;
font-weight: bold;
border-style: dashed;
}
button{
margin:3px;
color: black;
font-weight: bold;
font-size: 25px;
cursor: pointer
}
</style>
</head>
<body>
<table id="table" border="1">
<tr>
<th>S.NO</th>
<th>TITLE</th>
<th>ACTION</th>
</tr>
<tr>
<td>1</td>
<td>Title 1</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>2</td>
<td>Title 2</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>3</td>
<td>Title 3</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>4</td>
<td>Title 4</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>5</td>
<td>Title 5</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>6</td>
<td>Title 6</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
<tr>
<td>7</td>
<td>Title 7</td>
<td>
<button onclick="upNdown('up');">↑</button>
<button onclick="upNdown('down');">↓</button>
</td>
</tr>
</table>
<script>
var index; // variable to set the selected row index
function getSelectedRow()
{
var table = document.getElementById("table");
for(var i = 1; i < table.rows.length; i++)
{
table.rows[i].onclick = function()
{
// clear the selected from the previous selected row
// the first time index is undefined
if(typeof index !== "undefined"){
table.rows[index].classList.toggle("selected");
}
index = this.rowIndex;
this.classList.toggle("selected");
};
}
}
getSelectedRow();
function upNdown(direction)
{
var rows = document.getElementById("table").rows,
parent = rows[index].parentNode;
if(direction === "up")
{
if(index > 1){
parent.insertBefore(rows[index],rows[index - 1]);
// when the row go up the index will be equal to index - 1
index--;
}
}
if(direction === "down")
{
if(index < rows.length -1){
parent.insertBefore(rows[index + 1],rows[index]);
// when the row go down the index will be equal to index + 1
index++;
}
}
}
</script>
</html>