tags:

views:

51

answers:

4

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!

+3  A: 

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.

T.J. Crowder
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?
patrick dw
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
@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?
T.J. Crowder
@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.
T.J. Crowder
Thanks TJ, that's a super helpful example!
cdb
@cdb: Good deal, glad that helps!
T.J. Crowder
I just noticed, works in IE $("ul a span", "#menu").not(":contains['{Developers}']['{Developers Menu}']").css('color', 'rgb(210,210,210)').hover( but not Firefox. I added that second selector in there based off of what you exemplified. Did I do that correctly? I guess not since FF isn't playing.
cdb
@cdb: You've added square brackets to the bit I gave you which shouldn't be there. I don't know what you're trying to do with the second part you've added.
T.J. Crowder
I just want two different selectors, both of those are content tabs that I don’t want affected by the function. -I was wondering about the square brackets. I saw another example somwhere with them, I thought mabye they were to be used with concatenated elements. It seemed to work ,that is until I checked FF.
cdb
You can chain `.not` calls: `$("ul a span", "#menu").not(":contains({Developers})").not(":contains({Developers Menu})").css` or of course `$("#menu ul a span:not(:contains({Developers}):not(:contains({Developers Menu})").css`
T.J. Crowder
Wow, perfect! Thanks again TJ. You're a superb teacher.
cdb
+1  A: 

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); }
        );
  }
Mike Fielden
Or if you meant what TJ said... that is correct...
Mike Fielden
+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*...
T.J. Crowder
(She) did mean the .not function :)
cdb
A: 

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

Alexander Sagen
This brings up a good question, is a selector considered separate from a function, being that it's inside a function?
cdb
So, what I have now, just to be clear is: if (!($.browser.msie }, function(){ $(this).animate({color: 'rgb(210,210,210)'}, 200); } );
cdb
+1  A: 

Hi cdb,

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.

Rubens Mariuzzo