tags:

views:

185

answers:

5

Hi all,

I feel confused of the dot and hash symbols in the following example:

<DIV ID="row">
<DIV ID="c1">              
<Input type="radio" name="testing" id="testing" VALUE="1">testing1
</DIV>
</DIV> 

Code 1:

 $('#row DIV').mouseover(function(){     
    $('#row DIV').addClass('testing');  
    });

Code 2

  $('.row div').mouseover(function(){
        $(this).addClass('testing');
    });​

Codes 1 and 2 look very similar, and so it makes me so confused that
when I should use ".row div" to refer to a specific DIV instead of using "#row div" ?

+2  A: 

"." refers to a class, while "#" refers to IDs.

<table id="table">
    <tr class="odd"></tr>
    <tr></tr>
    <tr class="odd"></tr>
</table>

$("#table") would get the full table object, while $(".odd") would get everything with the class "odd". $("tr.odd") would only get table rows with that class.

Chris Doggett
+1  A: 

The . specifies a class called "row." The # specifies an id called "row."

EAMann
+5  A: 

$('.row') will select any element with class="row"

$('#row') will select the element with id=row

Check the jQuery page on selectors.

Matthew Jones
+14  A: 

The hash (#) specifies to select elements by their ID's

The dot (.) specifies to select elements by their classname

You can read more about the selectors here: http://api.jquery.com/category/selectors/basic-css-selectors/

Jeff Schumacher
Additionally, `#` and `.` are the same as they are in CSS.
R. Bemrose
+3  A: 

These are CSS selectors.

The hash symbol # means that the element is an ID. So #row would match <div id="row">.

Alternatively, the dot symbol . means the element is a CSS class. So .row would match <div class="row">.

There is more information over at W3C.

Justin Ethier
+1 for pointing out the xref between jQuery and CSS - I'm sure the jQuery authors chose these selectors intentionally to map to the existing standard for CSS.
GalacticCowboy
jQuery actually uses sizzle which is a css selector engine in javascript: http://sizzlejs.com/
ordnungswidrig