I want to take input from user into input field and after extracting the value, I want to add this in a js object with key. My code:
<!DOCTYPE html>
<html>
<body>
<input type="text" id="task">
<button onclick="add_ele()">add</button>
<p id="para"></p>
<script>
var l={}
var count=0;
function add_ele(){
var x = document.getElementById("task").value;
var count=count+1;
var pair= {count:x};
l={...l,...pair};
document.getElementById("para").innerHTML = JSON.stringify(l);
}
</script>
</body>
</html>
Output which I am getting:
The desired output is
l={1:"some message from input text", 2:"some message from input text", .. and so on}
But output which I am getting is {"count":"some text"}
Why this count is not replaced by count value which I am incrementing?