I have the following HTML code:
<div>
<input id="input1" type="text" size="50" autofocus autocomplete="on">
</div>
<div>
<input id="input2" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input3" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input4" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input5" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input6" type="text" size="50" autocomplete="on">
</div>
Now, when a user types something in any input field and then press enter, I need to recognize which one received the input, then store that input's ID in a var and the typed content into another. So far I have this code which I use to recognize if enter has been pressed:
window.onload = function() {
searchinput = document.getElementById("input1");
if(!!searchinput) {
searchinput.addEventListener("keypress", function(a) {
var key = a.keyCode;
if(key == 13) {
var query = this.value;
search(query);
}
});
}
};
But as you can see, it only works with a specific input field (input1 in the example), and it doesn't insert anywhere the element's ID.