How to add list of array through input element, make sure not allowing entered values and to display them in below?

Manideepgoud
Posted by Manideepgoud under JavaScript category on | Points: 40 | Views : 95
Hi All,

In the below code snippet, Am trying to show how to add list of values in the array and not allowing once entered values and to display them in the below paragraph.
<!DOCTYPE html>
<html>
<head>
<title>Javascript</title>
</head>
<body>
<input type="text" placeholder="Add" id="myvalue" class="searchinput" />
<button type="submit" onclick="myfunction()">Add</button>
<p id="demo"></p>
</body>
<script>
var myobject = {
name: ["Ram", "Ravi", "Ramesh"]
}
var myjson = JSON.stringify(myobject);
document.getElementById("demo").innerHTML = myjson;
function myfunction() {
if (myobject.name.includes(document.getElementById("myvalue").value)) {
debugger;
alert("Value exists!")
} else {
myobject.name.push(document.getElementById("myvalue").value);
document.getElementById("demo").innerHTML = JSON.stringify(myobject) + "<br/>";
}
}
var input = document.getElementById("myvalue");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
myfunction();
}
});
</script>
</html>

Comments or Responses

Login to post response