and

Why does jquery class selector select elements that have part of the class name?

if I have:

<div class="carBig"></div>

      

and

<div class="car"></div>

      

and $ (". car"). size ();

I get 2 items.

+1


source to share


2 answers


I think you might have something funky where it gets thrown away. If I run this very simple example, it works as expected.

<html>
<head>
</head>
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".car").hide();
    });
</script>
<body>
    <div id=container>
        <div class="carBig">Car Big</div>
        <div class="car">Car</div>
    </div>
</body>
</html>

      



You can try posting the rest of your html to see if we can figure it out.

+1


source


Which version of jquery are you using?

Using this code:

<html><head><title>Testing</title>
<script type="text/javascript" src="/js/jquery/jquery-1.2.6.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
    $(".car").each(function() {
        $("#carResults").append($(".car").size());
        $("#carResults").append($(this).text());
    });
});
</script>
</head><body>
<div class="carBig">Big Car</div>
<div class="car">Regular Car</div>
<div id="carResults"></div>
</body></html>

      



My output document looked like this:

Big Car
Regular Car
1Regular Car

      

Mine just found 1 item, the one with the class "car" ...

+4


source







All Articles