Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Difference in jQuery Selectors?

Here's a list of selectors I've seen before:

  1. $('.menu')
  2. $('menu')
  3. $('#menu')

Could anyone clarify in what scenario(s) each would be used?

like image 275
FLOOD racer Avatar asked Oct 05 '11 16:10

FLOOD racer


2 Answers

  1. $('.menu') ... select elements with class='menu'

  2. $('menu') ..... select <menu /> elements

  3. $('#menu') ... select an element with id='menu'

like image 104
Terry Avatar answered Oct 28 '22 15:10

Terry


1st finds <div class="menu"></div>

2nd finds <menu></menu>

3rd finds <div id="menu"></div>

Note that these rules applies and are based on CSS.

like image 36
Robin Castlin Avatar answered Oct 28 '22 17:10

Robin Castlin