2

When looking through jQuery examples, I see CSS classes referred to with a prepending "." sometimes and other times without. For example, in this snippet from codecademy:

else if(event.which === 110) {
  var currentArticle = $('.current');
  var nextArticle = currentArticle.next();

  currentArticle.removeClass('current');

Why is the selector $('.current') needed in the 2nd line, but only ('current') is required in the 4th line?

1
  • a method that takes a selector as an argument has the . first, a method that takes a class (or class list) as an argument will not have a dot Commented Apr 23, 2015 at 2:10

6 Answers 6

1

That is a convention.

On the 2nd line $('.current') is telling the jQuery to search for something called "current" and the dot specifies it as a class. So we need to call ".current"

On the 4th line, you're already telling jQuery that you are going to select a class by using "removeClass" so you don't need to use the dot there. Because that would be like "removeClass current being a class"

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

2 Comments

So it is necessary to use the '.current' convention at least once for jQuery to recognize which class you are referring to, then you can refer to it as 'current'?
Not quite that way. If you're using a selector (searching for an element) you will need to specify if its is a class (using ".") or an id (using "#"). But if you're doing any operation on the element you've already found, then you don't need to use those anymore.
1

In $('.current')(or in methods like find/next etc where you are trying to find an element with the given class), we are using a selector, so the notation for class selector is preceeding . has to be used.

When when you add/remove/toggle a class, you are not using a selector, you are just using a name so there is no . used

1 Comment

So it is necessary to use the '.current' convention at least once for jQuery to recognize which class you are referring to, then you can refer to it as 'current'?
0

The method explicitly uses the term Class.

Otherwise, you will always need a class selector, if that's your approach to targeting an element (you can target by ID, data attributes, HTML tags).

Comments

0

I think

var currentArticle = $('.current');

here you need a dot because it means you select it by class matching.

and in below:

currentArticle.removeClass('current');

you already specify to remove a class, you don't need a dot, because here it isn't a selector, it is the class name you want to remove.

Comments

0

removeClass() AND $('.current') are both method and you can pass parameters to an method, the only thing is that its the convention that jQuery uses. Try naming you class as <div class='.current'>I am a div</div> and try this console.log($('..current').text()); you will have this error Error: Syntax error, unrecognized expression: ..testA so basically its the rules / conventions of jQuery.

Comments

0

jQuery selectors have designed on CSS property, so same syntax could be used, you can also use class selector different way like $("[current]") it will work as HTML tag element properly.

function removeClass() not taking class as selector, the function announce that is only use for class properly so no need to determine passing value, although the function is taking value as string not <selector>

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.