0

***UPDATED JSFIDDLE (this is a more accurate representation of what I am dealing with -- second box shouldn't open if the open one is not valid)

NEW JS FIDDLE

I have searched and searched for this answer, even the ones that are opposite (like why isn't my click happening after the blur) don't answer my question. Hopefully someone can clarify.

I am using a proprietary API. There is a click event that is registered on this element I'm interested in. I need to do some work before this click and depending on the outcome of this work, I may not even want this click to happen. Obviously I cannot change the inner working of the API, so I am left with having to do something else.

I have tried just adding another click, but it won't call it first before the second click (in the API) because from what I read, they are called in the order of registration and the API registers theirs first.

I have tried a blur, which does get called before their click event, but preventDefault(), stopPropagtion() and stopImmediatePropagation(), and return false have done nothing to stop that click event.

Pretty much my method is a validation that if its good, the API's click event should happen... but if its not, then I need to to "not" click.

Besides using unbind with jQuery (which I haven't tested because I'd rather use native Javascript), I am unsure how I can do this.

here is JSFIDDLE

var d = document.getElementsByClassName('testDiv');

for (var i = 0; i < d.length; i++) {
   d[i].addEventListener('click', function(e) { 
        //alert('hi');
      e.target.style.backgroundColor = 'blue';  //this shouldn't happen if not valid
  });
   d[i].addEventListener('blur', function(e) { 
      //alert('I\'m blurred now');
      var valid = false
      if (!valid) {
        //e.stopImmediatePropagation(); //didn't work
        console.log('not valid');
        //return false;  //didn't work
    
    /***  if its not valid we want to cancel the click event ***/
  }
  else {
        e.target.style.backgroundColor = '#c486d0';
    e.target.innerHTML = 'Blurred';
  }
   });
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
carinlynchin
  • 389
  • 1
  • 3
  • 13
  • have you tried, `return false` and `event.stopPropagation`? – Val Apr 19 '16 at 13:34
  • I just looked at your JS fiddle, the problem is, that a `div` does not have a blur event, even if it did, it would not make sense, because blur would mean losing focus, what you want is `mousedown`, and `mouseup` events – Val Apr 19 '16 at 13:37
  • yes @Val, I have and nothing. Also, you can make it have a blur event if you add tabIndex attribute, which I did -- check jsfiddle (if you mousedown, you will see that the blur event does get called) – carinlynchin Apr 19 '16 at 13:44
  • I think, something like this.... is what you maybe looking for. https://jsfiddle.net/dcq20or2/1/ – Val Apr 19 '16 at 14:00
  • alternatively read about this as an alternative solution, http://stackoverflow.com/questions/9052349/jquery-events-prepend-a-callback-handler-to-already-existing-ones – Val Apr 19 '16 at 14:03
  • Thanks @Val but that isn't exactly what I want. the function in blur event handlers should stop the click..thats what i'm trying to accomplish. So pretty much once a div is blurred, the one being clicked on should NOT turn blue – carinlynchin Apr 19 '16 at 14:05
  • it turns blue, because this.valid is turned to true, you will need to pass your validation first and only then you do the `this.valid = true` that was as demonstration. I think you really want the alternative I have linked. – Val Apr 19 '16 at 14:08
  • You have to pretend that the click event is NOT touchable...that is what is inside the api, so I cannot change that...only can edit the blur event...just need the blur to stop the click. Use my original jsfiddle because that valid is set to false...let me edit it ...give me a sec – carinlynchin Apr 19 '16 at 14:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109567/discussion-between-carine-and-val). – carinlynchin Apr 19 '16 at 14:25

0 Answers0