I'm using onbeforeload to make sure users don't navigate away from the page without saving. Although I want it to trigger the event only if they have clicked in and input box (tbox). Here's my code.
Just set a boolen variable to False. When they click on an input, set it to True. On unload, check to see if the variable is True. If so, run your code. If not, unload the doc.
Well, I feel this is probably a school assignment, so I hate to code it for you. Just think about this: when your document loads, make a var called something like "editsMade". Set that to false. Create a function that will change editsMade to true. On your inputs, for the onclick events, call the function that will change your boolean variable to true. On your unload, check to see if editsMade is true or false. Do the appropriate action depending on that. This will suffice your need to see if the user clicked on an input. This will not check to see if the user actually made any changes, but it will warn them changes might have been made, which sounds like all you need.
$(document).ready(function(){
$("input[type='text'], select, textarea").change(function() < this line fixed me
{
window.onbeforeunload = function()
{
return "Your changes may not be saved.";
}
});
$("form").submit(function(){
window.onbeforeunload = null;
});
});
Bookmarks