So I have created a simple CRUD-application using only JavaScript. Right now you can add countries to the array and delete countries from the array. I want to be able to edit existing countries in the array, say for example I want to change "Stockholm" to "Spain".
What would that edit function look like? Below is my current code, html and javascript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
</body>
</html>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += stader[i] + "[<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>]<br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit() {
}
You can add a click function to each item and when you click on the text it opens a prompt window to change the text.
I changed the uppdateraOutput function to add a click function to the text.
<span onclick='edit("+ i +")'>" + stader[i] + "</span>
Like the code below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
<script>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += "<span onclick='edit("+ i +")'>" + stader[i] + "</span> [<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>] <br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit(index) {
var item = prompt("Please enter your name", stader[index]);
if (item !== null && item !== "") {
stader[index] = item;
}
uppdateraOutput();
}
</script>
</body>
</html>