1

I have checked a few answers on SO and I think my syntax is correct, but something is not working correctly.

I am trying to create an array of jquery selectors mixed with variables and store them in a not() function.

I have this fiddle to demonstrate.

The second line of the function is not working:

var trigger = $('mainNav li:eq(0) a');
var headerLinks = $('#header a').not('.control a', + trigger);

My first thought was that #header a has more precedence over .control a in css terms. But changing classes and ID's doesn't seem to work.

Similarly I have a simpler var further in my code:

var content = $('.content');
var added = $('.extraList', + content);

In the console added is only returning the extraList selector.

1 Does the not function work differently to other jquery selectors? I was hoping to see the array of all the elements selected and/or filtered in the console

2 Is there a way to store the vars and selectors in a standard array and call that? I don't know how this would look exactly as jquery would need to interpret it, but something like this:

var arr = ['.control a', headerLinks, content, '.post'];

I realise that is mixing types but I am not sure how to pass both jquery variables and selectors into the same array, if it is possible.

2 Answers 2

1

For code var headerLinks = $('#header a').not('.control a', + trigger);

You can simply use chaing like

var headerLinks = $('#header a').not('.control a').not(trigger);

OR

var trigger = $('.control a, #mainNav li:eq(0) a'); //I am using here #mainNav
var headerLinks = $('#header a').not(trigger);

For var added = $('.extraList', + content);

You can use .add()

Create a new jQuery object with elements added to the set of matched elements.

var added = $('.extraList').add(content);
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @Satpal I did try chaining the not functions but that did not seem to work. See updated fiddle: jsfiddle.net/lharby/js5okk49/4
@lharby, .control is not child of header so $('#header a').not('.control a') will still refer to $('#header a'). what are you trying to acheive?
I am trying to populate a list of links that should not trigger a certain function. The other links that are not specified should trigger the function.
OK well the add function works. Unfortunately the control a links are still logging 'was clicked' to the console when they should have been excluded. Ah OK, not(): Remove elements from the set of matched elements. So it runs the first selector as the parent. Thank you.
@lharby, See jsfiddle.net/mbe547o7 this will resolve your problem, main changes is var headerLinks = $('#header a').not(trigger);var contentLinks = $('.post a').not('.control a');
|
0

Thanks to @Satpal. Your answer definitely helped me, but here is what I did.

Store an array of jquery selectors to later exclude from the function, I can keep adding to or edit this variable. (I think this is neater as it has separated out the code a little more and it circumvents the parent/child issue):

var exclude = $('#mainNav li:eq(0) a, .control a, .social-links a');

Create another set of links to include in the function:

var headerLinks = $('#header a');
var contentLinks = $('.post a');

Merge selectors which are going to be used to trigger the function and also add the excluded selectors:

var merge = $.merge(headerLinks, contentLinks).not(exclude);

Now the click function:

merge.on('click',function(){});

Should fire for the links that I want, and not the ones that I don't.

I would still like to know how to store an array of jquery selectors and variables in a standard javascript array.

Updated fiddle: http://jsfiddle.net/lharby/gk7bmzqh/1/

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.