2

I have this code:

    $(window).scroll(function(){
        $('#globalnav').css('left',-$(window).scrollLeft());
        $('#globalborder').css('left',-$(window).scrollLeft());
        $('#backgroundlayer').css('left',-$(window).scrollLeft());
    });

How can I make al the variables compiled in just one line? So it should like something like this

    $(window).scroll(function(){
        $('#globalnav','#globalborder','#backgroundlayer').css('left',-$(window).scrollLeft());
    });
user3537202
  • 185
  • 1
  • 1
  • 9
  • Note that by keeping the `,` outside of the `'`, you are passing *multiple* arguments to the jQuery `$` function. Using adeneo's answer, you are passing a *single* argument to `$`, which is a string representing a list of selectors. – ajp15243 Apr 15 '14 at 18:33
  • possible duplicate of [jQuery Multiple ID selectors](http://stackoverflow.com/questions/7079011/jquery-multiple-id-selectors) – epascarello Apr 15 '14 at 18:34
  • 1
    @epascarello That looks like an issue with whatever `upload` plugin that questioner was using, rather than an issue selecting multiple items by `id`, despite the title. – ajp15243 Apr 15 '14 at 18:36
  • Than pick: http://stackoverflow.com/questions/488305/jquery-selecting-multiple-classes There are 100s of related ones. – epascarello Apr 15 '14 at 18:44
  • @epascarello I didn't say there weren't other related ones, and I'm certainly not stopping you from marking this as a duplicate. – ajp15243 Apr 15 '14 at 18:45

1 Answers1

9

Just drop some quotes

$(window).scroll(function(){
    $('#globalnav, #globalborder, #backgroundlayer').css('left',-$(window).scrollLeft());
});

If you pass the elements as a comma separated list in one single string to the selector, they are all added.

If you use a comma separated list of different strings, you're using the "context selector", which is a shortcut for find() and works differently.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • if we pass one selector in single quotes then comma as OP was doing what will happen actually – Ehsan Sajjad Apr 15 '14 at 18:35
  • @EhsanSajjad you can set up a fiddle that will allow you to test what happens with different selectors. – Jay Blanchard Apr 15 '14 at 18:38
  • Is it also possible to delete the # and say that that should al have that before? – user3537202 Apr 15 '14 at 18:38
  • @EhsanSajjad The `$`/`jQuery` function will attempt to match the arguments following one of its signatures, as seen in [the API docs](https://api.jquery.com/jquery/). However, none of its signatures takes 3 arguments, and as some quick console testing via dev tools revealed on the jQuery website, an empty jQuery object is returned. Presumably, this is the default behavior if it cannot match arguments to one of its signatures. – ajp15243 Apr 15 '14 at 18:38
  • The context selector calls find internally, so this `$('.child', '#parent')` is equal to `$('#parent').find('.child')`, of course trying to call that with three arguments fails. – adeneo Apr 15 '14 at 18:42
  • @EhsanSajjad Is it also possible to delete the # and say that they should al have that before with jquery? – user3537202 Apr 15 '14 at 18:42
  • @user3537202 - no it's not, they all have to have the ID hash in front. – adeneo Apr 15 '14 at 18:42
  • @user3537202 It is not, each ID will need the `#` preceeding it. Think of how you would select multiple items separately in CSS selectors by ID (CSS selectors is what jQuery selectors are modeled after). – ajp15243 Apr 15 '14 at 18:44