Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Event Triggering - onBlur Firing Ahead Of onClick

    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.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    <html>
      <head>
        <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()
        {
          alert('Saved!');
        }
        function cancelSave()
        {
          window.clearTimeout(timer);
        }
        </script>
      </head>
      <body><input type="text" id="save"><input type="button" id="cancel" value="cancel"></body>
    </html>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dumpfi,

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •