2

the code:

 if (!($.browser.msie && $.browser.version.substr(0, 1) == '5')){
        $('ul a span', '#menu'.not('{Developers}')).css('color', 'rgb(210,210,210)').hover(
            function(){ $(this).animate({color: 'rgb(255,255,255)'}, 500); },
            function(){ $(this).animate({color: 'rgb(210,210,210)'}, 200); }
        );
  }

Does this look like correct syntax? It's working, however it's throwing the error that .not is not a function. I'm calling this function in a menu.js that my main page is loading. It's not in a document ready, and it is working, other than the error. If it weren't working, I'd say my core jq library wasn't loading first, but it is. Help is appreciated! Note, :not does not work, so please don't suggest I use that. Thanks!

2
  • Could you post your html code too ? Edit your question to do so please. Commented Sep 28, 2010 at 21:15
  • 1
    Can you give an HTML example of what you are trying to select? It would be unusual to use not() against an ID attribute, since IDs must be unique. Commented Sep 28, 2010 at 21:17

4 Answers 4

3

If you mean to be using the jQuery function .not, you need to call it on a jQuery object (currently you're trying to call it on a string). Here' an example:

$('ul a span', '#menu').not('{Developers}').css
                      ^--- changes here ---^

If you really mean selector as in your title, then it's :not, not .not (reference), and it would need to be part of the selector string. But you said later in your question that you tried :not and it didn't work (I wasn't sure where or how you tried :not, though).

I think there's another problem with that code, because both .not and :not expect a selector (.not also allows an element/jQuery instance or function), and the selector '{Developers}' is invalid.

From your comment that it's working other than the exception, I can only surmise that something else is changing the background on the elements, because the code as quoted will fail (throw an exception, that is barring your having added a not function to String.prototype), and won't enter the $ function at all, much less get to the css part of it.

If your goal is to color the background of the spans matching the selector ul a span under the element with the ID menu but not the span containing the text "{Developers}", you need to use the :contains pseudo-class (taken out of CSS3 but jQuery supports it):

$("#menu ul a span:not(:contains('{Developers}'))").css(...

Live example

If you prefer the form that passes #menu as a context parameter instead:

$("ul a span", "#menu").not(":contains('{Developers}')").css(...

Live example ...but jQuery will handle them both efficiently.

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

10 Comments

T.J. - I have a feeling that your first inclination was right, since the question states "Note, :not does not work, so please don't suggest...". Makes it seem like what was tried was '#menu:not(...'. But who knows?
Yes, :not was tried. The {Developers} is literal content- it's a navigation title. Again, it does work, so the .not is being understood, everything but my {Developers} title is being colored light gray. However, fixing that extra parenthesis still didn't make the error go away.
@cdb: Unless you yourselves are adding a not function to the String.prototype, the code you've quoted throws an exception at '#menu'.not(. Something else may make it appear to work, but again without your having added a not function to String, the code you've quoted doesn't even go into the $ function at all. Stepping back, what is the stuff prior to the css supposed to select?
@cdb: Actually, from your comment above, I think I can guess what you're trying to do. I've updated the answer to do that.
Thanks TJ, that's a super helpful example!
|
1

I can see a typo in you code.

if (!($.browser.msie && $.browser.version.substr(0, 1) == '5')){
  $('ul a span', '#menu').not('{Developers}').css('color', 'rgb(210,210,210)').hover(
    function(){ $(this).animate({color: 'rgb(255,255,255)'}, 500); },
      function(){ $(this).animate({color: 'rgb(210,210,210)'}, 200); }
    );
}

In your code you have

 $('ul a span', '#menu'.not('{Developers}')).css...

but it should be

$('ul a span', '#menu').not('{Developers}').css...

You are missing one parenthesis and you need one too.

Comments

1

Assuming you pasted the code correctly, you're missing a ')'

jQuery .not() info

if (!($.browser.msie && $.browser.version.substr(0, 1) == '5')){
        $('ul a span', '#menu').not('{Developers}').css('color', 'rgb(210,210,210)').hover(
            function(){ $(this).animate({color: 'rgb(255,255,255)'}, 500); },
            function(){ $(this).animate({color: 'rgb(210,210,210)'}, 200); }
        );
  }

2 Comments

+1 Yeah, you got there faster than I did (though I did get there). I kept staring at the expression trying to make a CSS selector out of it. Then I finally realized the second part (#menu) was a context, and realized he meant the .not function. I got so stuck on selectors...
(She) did mean the .not function :)
0

... $('ul a span', '#menu').not( .... Missing a parenthesis I think? Or was it supposed to be part of the selector?

2 Comments

This brings up a good question, is a selector considered separate from a function, being that it's inside a function?
So, what I have now, just to be clear is: if (!($.browser.msie && $.browser.version.substr(0, 1) == '5')){ $('ul a span', '#menu').not('{Developers}').css('color', 'rgb(210,210,210)').hover( function(){ $(this).animate({color: 'rgb(255,255,255)'}, 500); }, function(){ $(this).animate({color: 'rgb(210,210,210)'}, 200); } );

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.