5
<input id="ok" /> 

$('.hor-minimalist-a').on('blur','#ok',function(){

});

with this code above, i can handle events which fire If i leave the input field.

How can I detect if someone clicks outside of inputfield without coming to input. I mean the user never came to this input field before, so there was no focus at all.

$('.hor-minimalist-a').on('?','#ok',function(){
  ?
});
doniyor
  • 36,596
  • 57
  • 175
  • 260

3 Answers3

6
$(document).on('click', function(e) {
    if ( e.target.id != 'ok' ) {
        // you clicked something else
    }
});

That would capture any click except on the input, but why would you need it ?

To capture only clicks on .hor-minimalist-a and not the entire document, just replace document with that selector in the code above.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • i need it, because i have an edit icon where if user clicks a new input field shows up. on blur, the input fields shuts down, but i suspect also, that after hitting edit-icon, the user can decide not to edit. so in this case, on any click outside, the inputfield should shut down – doniyor Aug 06 '14 at 14:16
  • 1
    Then you should set up an example, and ask for that specifically, as that would probably require setting a flag wnen the edit icon is clicked, and then check for the next click etc. The best thing would probably be to just focus the input when the edit icon is clicked, and capture the blur event and then check the value. – adeneo Aug 06 '14 at 14:17
  • oh man, this is the best option (setting the input to focus). i will go for it :) – doniyor Aug 06 '14 at 14:18
1

How about:

$('.hor-minimalist-a').on('click',':not(#ok)',function(){

That'll register click events within .hor-minimalist-a but outside the input #ok

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

There is a jquery plugin for that: http://benalman.com/code/projects/jquery-outside-events/examples/clickoutside/

Leif
  • 1,076
  • 10
  • 16