What do dot and hash symbols mean in JQuery?

Jquery

Jquery Problem Overview


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" ?

Jquery Solutions


Solution 1 - Jquery

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/

Solution 2 - Jquery

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

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

Check the jQuery page on selectors.

Solution 3 - Jquery

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.

Solution 4 - Jquery

"." 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.

Solution 5 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser327712View Question on Stackoverflow
Solution 1 - JqueryJeff SchumacherView Answer on Stackoverflow
Solution 2 - JqueryMatthew JonesView Answer on Stackoverflow
Solution 3 - JqueryJustin EthierView Answer on Stackoverflow
Solution 4 - JqueryChris DoggettView Answer on Stackoverflow
Solution 5 - JqueryEAMannView Answer on Stackoverflow