2

consider following,

<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="txt1" />
            <input type="button" id="btn1" value="Submit"/>            
        </div>
    </form>
    <script>         
        $("#txt1").live("blur", function () {
            console.log('blur');            
            return false;
        });

        $("#btn1").live("click", function () {
            console.log('click');
            return false;
        });
    </script>
</body>

Above code will log blur event and click event on trigger of respective events. If click or change something in text box and then click on button btn1 blur and click event will happen respectively.What i want is if blur event is happening because of btn1 then click event should not happen,it should log only blur event,I want to stop click event from happening. How to do this? Can anyone help?

OptimizedQuery
  • 1,262
  • 11
  • 21
Yogesh
  • 33
  • 1
  • 3

4 Answers4

1

try this

<form id="form1" runat="server">
   <div>
      <input type="text" id="txt1" />
      <input type="button" id="btn1" value="Submit"/>            
   </div>
</form>

javascript code

$("#txt1").on("blur", function (event) {
   event.preventDefault();
   alert('blur');            
   return false;
});
$("#btn1").on("click", function (event) {
   event.preventDefault();
   alert('click');
   return false;
});

also test it here and remember live keyword is deprectaed from jquery 1.9 use on instead of live in jquery 1.9 or greater.

user2727841
  • 715
  • 6
  • 21
0

You cannot "stop" an other/foreign event like so. Event.preventDefault() and/or Event.stopPropagation() (which both will get triggered when returning false from within a jQuery event handler), will allow you to stop and prevent the exact same event from further processing on parent nodes.

In your instance, you need your own logic. Use some variables and set them properly and check the value where necessary. For instance, on click you set FOOBAR = true and in blur you check if( FOOBAR ) and act on that.

jAndy
  • 231,737
  • 57
  • 305
  • 359
0

You need to destroy one event see the demo

Hope this helps you.

jsfiddle.net/rkumar670/5a86V

Rahul Kumar
  • 528
  • 1
  • 3
  • 19
0

Here is one way to solve it by adding a timeout.

var inFocus = false;

$("#txt1").focus(function () {
    inFocus = true;
    $("#log").prepend("<p>focus</p>");

});
$("#txt1").blur(function () {
    setTimeout(function(){inFocus = false;},200);
    $("#log").prepend("<p>blur</p>");
});
$("#btn1").click(function () {
    if (!inFocus) {
        $("#log").prepend("<p>click</p>");
    }
});

In the fiddle example, I put the log out to the window.

Chi Row
  • 1,106
  • 7
  • 18