View Full Version : Event Triggering - onBlur Firing Ahead Of onClick

08-15-2008, 06:36 AM
I am having a problem with function triggering. Basically I have a function save() that gets called onBlur of a textfield.

<input type="text" id="status" value="" onBlur="save()" />

I also have a cancel button that triggers a function cancel() onClick.

<input type="button" id="cancel" value="Cancel" onClick="cancel()" />

The problem is that if you try to click the cancel button, it triggers the save() because the field blurred before it recognized the onClick of the cancel button. I need to write some code at the start of save() which waits and checks if the onClick handler was fired, thus calling cance(), before proceeding with the save.

Thanks for the help with solving this problem.

08-15-2008, 09:14 AM
<script type='text/javascript'>
window.onload = function()
document.getElementById('save').onblur = delayedSave;
document.getElementById('cancel').onclick = cancelSave;

function delayedSave()
timer = window.setTimeout(save, 50);
function save()
function cancelSave()
<body><input type="text" id="save"><input type="button" id="cancel" value="cancel"></body>

08-15-2008, 09:33 AM

Thanks that seemed to work, except the timeout period of 50 milliseconds wasn't enough, sometimes it would still call save() on the click of cancel. I changed the timeout to 250 and it seems to work well. Do I really need to call clearTimeout(timer) on entry of the cancel function though?

Thanks for the help.