Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Mobile Drop Down Menu Perform: Part 1

Hossam Khalifa
Hossam Khalifa
17,200 Points

I don't understand the syntax of jQuery

When to use "a" when a when "href" or href when $(variablename) when only variablename its is very confusing.

5 Answers

Paolo Scamardella
Paolo Scamardella
24,828 Points

your question is a little bit of confusing. Let me see if I can help... Let say you have:

<a id="idLink" href="link.html" class="classLink">Link</a>

$("#idLink") getting the element by using the id of the element $(".classLink") getting the element by using the class of the element $("a") getting the element by using the tag of the element

There is no difference and it stills gets the element you are looking for. It is up to you how you want to grab it, but keep in mind if you have more than one a tag elements and using this syntax, $("a"), it will grab all a tags on your page.

Hossam Khalifa
Hossam Khalifa
17,200 Points

Thank you,and when do I use the tag without quotes ?? or is this just my imagination ?

Janek Rezner
Janek Rezner
12,973 Points

when you go do

$("#something")

you tell jQuery to go to the DOM and search for a HTML element and so you need to use "" or ''

if you decide to cache it in a variable its a part of JavaScript and jQuery doesn't need to search for it.

you don't need to use quotes.

Paolo Scamardella
Paolo Scamardella
24,828 Points

I believe older version of jQuery will not work without quotes; it will work on never versions. However, it is always good practice to stick with using single or double quotes(just be consistent in your code), and there is a possibility that some browsers will not accept selectors without quotes.

Bottom line: always use quotes either single or double.