How to add and remove input values in Javascript

Manideepgoud
Posted by Manideepgoud under JavaScript category on | Points: 40 | Views : 1904
<!DOCTYPE html>
<html>
<head>
<title>Javascript</title>
</head>
<body>
<input type="text" placeholder="Add" id="myvalue" class="searchinput" />
<button type="submit" onclick="addFunction()">Add</button>
<button type="submit" onclick="deleteFunction()">Delete</button>
<p id="demo"></p>
</body>
<script>
var myobject = {
name: ["Ram", "Ravi", "Ramesh"]
}
var myjson = JSON.stringify(myobject);
document.getElementById("demo").innerHTML = myjson;

function addFunction() {
if (myobject.name.includes(document.getElementById("myvalue").value)) {
alert("Value exists!")
} else {
myobject.name.push(document.getElementById("myvalue").value);
document.getElementById("demo").innerHTML = JSON.stringify(myobject) + "<br/>";
}
}
function deleteFunction() {
var index = myobject.name.indexOf(document.getElementById("myvalue").value);
if (index > -1) {
myobject.name.splice(index, 1);
}
document.getElementById("demo").innerHTML = JSON.stringify(myobject);
}
// var input = document.getElementById("myvalue");
// input.addEventListener("keyup", function(event) {
// if (event.keyCode === 13) {
// addFunction();
// deleteFunction();
// }
// });
</script>
</html>

Comments or Responses

Login to post response