1

I am having a devil of a time getting two selectors to work together as well as do when I declared seperate.

My issue:

I want to close a menu when I move to any input OR anchor tag that is NOT contained with a certain element. When I explicitly code for both input and anchor, I get the behavior I need - BUT when I try to condense my code and combine them, it behaves weirdly and does not work as intended.

So, basically when a user focus's in an input field or anchor that is NOT a child of my selector I want to close a menu. When I use two sperate handlers, it works. I want to combine them.

I am trying to shorten this....

jQuery('#hdr input:not(#opt *)')
.focusin(function(event){
   setTimeout(function(){
       jQuery("#opt").hide()
   },100);
 });
jQuery('#hdr a:not(#opt *)')
.focusin(function(event){
   setTimeout(function(){
       jQuery("#opt").hide();
   },100);
 });

I've tried all of this into one line, to no avail:

jQuery('#hdr a input:not(#opt *)')
jQuery('#hdr a, #hdr input:not(#opt *)') <-- I expect this to work, but doesn't.
jQuery('#hdr a,input:not(#opt *)')
jQuery('#hdr *:not(#opt *)')

It seems to only work when I do a single arg like: #hdr a , or #hdr input BUT when I try to combine them, no luck. I've searched high and low but no luck.

2 Answers 2

4

You can use the not method:

$('#hdr').find('a,input').not('#opt *')

Sometimes it's just better and more readable to use the methods instead of a huge selector string.

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

1 Comment

yeah - gosh I was "gonna" use find but not sure why I didn't event bother with it. Your way worked great! Thanks a bunch.
2

All the elements are child of #opt and not selector ensure that #opt which is parent and all the elements or nodes in it are prevented from event handler. Like this in Fiddle:

Working Fiddle

Also The Method of @elclanrs works.

$("#hdr a:not(#opt), #hdr input:not(#opt)")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.