views:

66

answers:

5
$(function () {


   var sr = 'section.row';
   var fot = 'figure.one_third';
   var pA = 'div.portfolio ul li a'; 

   var item = ('sr fot pA');

   $item.addClass('blue');

});

http://jsfiddle.net/G8yJj/13/ <<

+1  A: 

There's no need for any extra quotes.

Just use var $item = $(pA);

Marko
A: 

I think I see two things:

  1. Javascript variables must begin with a letter or _ (not a $) (edit: never mind)
  2. You have extra single quotes in the $ selector, so instead of searching for div.portfolio ul li a, it's searching for the string " + pA + "
Tom Smilack
That isn't true on (1)., variables that begin with `$` are very common with jQuery. `$` doesn't make a variable special either, it just denotes a jQuery object by convention.
Evan Carroll
So it does work. But why would w3schools lie to me!?
Tom Smilack
A: 

As cambraca said in a comment, no quotes.

$(function () {

   var pA = 'div.portfolio ul li a'; 

   var $item = $(pA);

   $item.addClass('blue');

});

You do write

   var $item = $("div.portfolio ul li a");

But thats only because you need to have a way to mark the selector div.portfolio ul li a as a non-javascript query. If you explicitly make a String with those contents, then quoting it would read like this.

   var $item = $('"' . '"div.portfolio ul li a"' .'"');

And, that doesn't make sense.

Evan Carroll
+4  A: 

the line needs to be:

var item = $(pA);
item.addClass('blue');

or in your updated question:

var item = $(sr + ' ' + fot + ' ' + pA);

if that's what you want.

動靜能量
cheers mate, I gave u the green tick because you answered it first. Although I appreciate the help from all of you.
Daryl
Hi @動靜能量, it's common practice to prefix jQuery object variables with a $. So `$item` instead of `item`.
Marko
+1  A: 

sr and fot are not getting evaluated as variables, they are just part of the string.

You need

$(sr + "," + fot + "," + pA)
Samo
As a side note, I think your variable names are very nondescript. Ask yourself if another developer would have any idea what those variable names mean. Or if you would have any idea what they mean 6 months down the road. Readability -> Maintainability my friend
Samo
lol they were temporary names just to make it short.
Daryl