locked
how to cancle click event of a button, which executes after lostfocus of textbox element. RRS feed

  • Question

  • User2102072086 posted

    hi,

        I have a textbox and a button, with this I have written a focusout event of textbox which calls a function which returns true or false.

    and I have written one click event of the button.

    now I want to cancel the click event of the button if the function which is called in focusout returns false else it should follow the natural chain of events.

    and the problem is worse because I have many buttons and checkboxes which have got click events or other events too. so I want to find the next event which can execute after focusout and  then cancel it  as per the case.

         

    Thursday, June 25, 2020 12:14 PM

All replies

  • User475983607 posted

    now I want to cancel the click event of the button if the function which is called in focusout returns false else it should follow the natural chain of events.

    and the problem is worse because I have many buttons and checkboxes which have got click events or other events too. so I want to find the next event which can execute after focusout and  then cancel it  as per the case.

    As far as I know there is not such thing as a standard "natural chain of events" in JavaScript.  There's recommendations but browser can implement anyway they like.

    You might find creating a simple test with a button and text input seems deterministic.  That all goes out the window when you have many event listeners registered.  You should consider all events as asynchronous and global variables can't be trusted.

    Thursday, June 25, 2020 1:35 PM
  • User-474980206 posted

    The focus events are not cancelable. You need to set a global variable. You will also need to define when to clear it, probable on text box focusin.

    Thursday, June 25, 2020 3:05 PM
  • User2102072086 posted
    I want to disable all the elements except the text box whose entry is wrong.
     , the problem is, whenever someone enters the wrong txt in the textbox , I set the focus to the element, I do not want the user to click any other control unless he corrects the entry which is mandatory.
    Saturday, June 27, 2020 8:45 AM
  • User1535942433 posted

    Hi rajemessage,

    As far as I think,when you focus and valicate the textbox,if the valication is false,you will could add disable attributes to other elements.

    Just like this:

    element.setAttribute("disabled", "disabled")

    Best regards,

    Yijing Sun

    Monday, June 29, 2020 9:01 AM
  • User-1151440187 posted

    I think you should use the onblur event of textbox.

    And set your logic in that if entry by the person in textbox is wrong then refocus textbox using 

    var element= document.getElementById('foo');
    element.focus();

    I hope this'll be helpful.

    Thanks!

    Tuesday, June 30, 2020 4:20 AM
  • User2102072086 posted

    that is not the question, it is working, the question is if I clicked on other button it would fire the both events , and I want to cancel the button click event.

    and the problem is we have more tan one buttons with click event,so I wanted to cancel the any next event which fires of any other button. after the focusout or onblure event  of text box.

    Tuesday, June 30, 2020 8:05 AM
  • User-474980206 posted

    The focus events are not cancelable only the click is. You will need to add code to the focus events to set a flag the on click handler uses to cancel the click event.

    Tuesday, June 30, 2020 2:33 PM