Check out this code snippet that ensures txtKeyword input is focused when a key is pressed by the user.
var txtKeyword = document.getElementById("txtKeyword");
...
document.addEventListener("keypress", function(event) {
if(event.srcElement == txtKeyword)
{
return;
}
txtKeyword.focus();
}
I want to determine whether the message sender matches the element I intend to focus on. How can I achieve this?
event.srcElement == txtKeyword
event.srcElement.id == "txtKeyword"
event.srcElement === txtKeyword
Which comparison method is quicker? Taking into account that id is a string, using lengthy element ids may not be ideal.