Code đây:
index.html:
<!doctype html>
<html lang="en">
<head>
<title>Javacsript</title>
<link rel="stylesheet" href="./index.css"></link>
</head>
<body>
<div class="main">
<div class="left">
<p>Nhap mot tu tieng anh:</p>
<p><input type="text" id="word"/></p>
<p><input type="button" id="submit" value="Kiem Tra" onclick="checkWord()"/><p>
</div>
<div class="right">
<textarea rows="30" cols="50" id="list">
</textarea>
</div>
</div>
<script src="./index.js" type="text/javascript"></script>
</body>
</html>
index.css:
body {
padding-top: 50px;
background-color: #000000;
margin: 0;
}
.main {
position: relative;
margin: auto;
padding: 10px;
width: 40%;
background-color: #ffffff;
height: 500px;
}
.right {
position: absolute;
right: -10px;
width: 60%;
}
.left {
position: absolute;
left: -10px;
width: 40%;
text-align: right;
}
index.js:
var data = ["Hello", "Welcome", "Hi"];
window.onload = function() {
document.getElementById("word").value = "";
refreshList();
}
var checkWord = function() {
var list = document.getElementById("list");
var word = document.getElementById("word");
var index = getIndex(word.value);
if (index != -1) {
if (confirm("Tu nay da co o muc " + (index + 1) + ", ban co muon xoa khoi danh sach?")) {
data.splice(index, 1);
refreshList();
}
} else {
data.push(word.value);
refreshList();
}
word.value = "";
}
var getIndex = function(word) {
for (var i=0; i<data.length; i++) {
if (word.toLowerCase() === data[i].toLowerCase()) {
return i;
}
}
return -1;
}
var refreshList = function() {
var list = document.getElementById("list");
list.innerHTML = "";
for (var i=0; i<data.length; i++) {
list.innerHTML = list.innerHTML + data[i] + "\n";
}
}