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?

2
  • see my (@user2727841) answer and link... If it fulfil your requirement then vote it for others for reference.... Commented Feb 4, 2014 at 13:25
  • Ok your problem solved... Commented Feb 4, 2014 at 13:32

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanxx,this works,but due to some compatibility issues I can't use 'on' function.So I can't refer to event.
And it's not working with console.log.
its working with console and for that you've see it in console.
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.

Comments

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.

Comments

0

You need to destroy one event see the demo

Hope this helps you.

jsfiddle.net/rkumar670/5a86V

3 Comments

I think your page has been removed.
Rahul,thanks for your response,but it will destroy click event permanently,and I don't want that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.