Let's consider the following scenario:
I have a situation where some pages use a master page. Some of these pages contain an update panel while others do not. I have a jQuery code that needs to be executed when a user triggers the keydown
event.
$(document).ready(function () {
$('.myClass').on('keydown', function (e) {
var MainCharKey = e.which.toString();
...
})
})
I want to include this code in my master page so that every textbox
with the myClass
css class uses this functionality. The problem arises when I place this code in the master page - on pages with an update panel, the code only works once (during the initial page load), and subsequent postbacks disable it. One attempted solution is to modify the code as follows:
function pageLoad(){
$(document).ready(function () {
$('.myClass').on('keydown', function (e) {
var MainCharKey = e.which.toString();
...
})
})
}
However, this approach causes the code to fail on pages without an update panel.
Where can I write this code so that it functions correctly on both pages with and without an update panel?
Thank you