0

Is there a way that you can check if there isn't a input selected (text input)

I tried

if(!$('input').is(":selected")) {
       alert('hoi');
};

but it doesn't work

I dont want to use focus out because if i'm going from textfield 1 to textfield 2 the alert comes, so I'm looking for a code that do: if all the inputs arn't focussed then the alert comes on the screen

j08691
  • 204,283
  • 31
  • 260
  • 272
Erwin
  • 111
  • 2
  • 11
  • 2
    how can more than one elements of type input will have focus on it?? – Milind Anantwar Mar 09 '15 at 14:30
  • that i dont want? i want that there comes a message when there is none of the textfields focussed. if im working with `focusout` and im going from textfield 1 to textfield 2 then he deselect first textfield 1 and then focus text field 2 only is it possible to use like this 'if(!$("#t1").focus() && $("#t2").focus() && $("#t3").focus(){ alert("hoi");}' – Erwin Mar 09 '15 at 14:34
  • Just set a variable to true on focusout and false on focus and check this variable – Robin Leboeuf Mar 09 '15 at 14:37
  • what about .focusin() ? – Vilas Kumkar Mar 09 '15 at 14:44
  • @VilasKumkar I tried but when i'm focussing a textbox and then cancel the focus there comes no message. – Erwin Mar 09 '15 at 14:53

1 Answers1

2

:selected only works for option elements.

See: http://api.jquery.com/selected-selector/

You want to use the :focus selector. A generally accepted way to test if elements exist on a page (in this case focused elements) is to use the .length attribute.

if(!$('input:focus').length) {
    alert("None selected")
}
Jordan Burnett
  • 1,799
  • 8
  • 11
  • it only works when no-one have selected any inputs but when someone focus an input and unfocus it, it doesn't works. – Erwin Mar 09 '15 at 14:41
  • Where are you running this code? You might want to check what element is being focused, does this fiddle help? http://jsfiddle.net/0rkpbLyn/ – Jordan Burnett Mar 09 '15 at 15:00