This solution was given to this question asking how to trigger an HTML button when Enter is pressed in an input field.
<input type="text" id="txtSearch" onkeypress="searchKeyPress(event);" />
<input type="button" id="btnSearch" Value="Search" onclick="doSomething();" />
<script>
function searchKeyPress(e)
{
// look for window.event in case event isn't passed in
if (typeof e == 'undefined' && window.event) { e = window.event; }
if (e.keyCode == 13)
{
document.getElementById('btnSearch').click();
}
}
</script>
Why is if (typeof e == 'undefined' && window.event) { e = window.event; }
nescecary? It appears to be checking if the argument didn't get passed correctly, why wouldn't it? Is this related to fixing browser compatibility issues?