-3

Possible Duplicate:
jQuery id selector works only for the first element

I want jquery to select all divs with id = box:

<!DOCTYPE html>
    <head>
    <link rel="stylesheet" href="css/main.css" type="text/css"/>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="js/sizer.js"></script>
        <title>
        Design tests
        </title>
    </head>
    <body>
        <div id="box" style="width:300px;">
        Hallo
        </div>

        <div id="box" style="width:300px;">
        Hallo
        </div>

    <script>    
    $(document).ready(function(){
        $("#box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
    });
    </script>
    </body>
</html>

But it only selects the first one. Can somebody please help me? This isn't difficult is it?

Community
  • 1
  • 1
Henry Mü
  • 91
  • 6

4 Answers4

14

HTML allows only one element with a given id. That's the reason (internally jQuery uses getElementById which returns one element).

From the spec :

id = name [CS]

This attribute assigns a name to an element. This name must be unique in a document.

If you want to select more than one element, use a class, not an id :

    <div class="box" style="width:300px;">
    Hallo
    </div>

    <div class="box" style="width:300px;">
    Hallo
    </div>

<script>    
$(document).ready(function(){
    $(".box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});
</script>
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
6

You cannot have more than 1 same ID, use a class instead

<!DOCTYPE html>
    <head>
    <link rel="stylesheet" href="css/main.css" type="text/css"/>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="js/sizer.js"></script>
        <title>
        Design tests
        </title>
    </head>
    <body>
        <div class="box" style="width:300px;">
        Hallo
        </div>

        <div class="box" style="width:300px;">
        Hallo
        </div>

    <script>    
    $(document).ready(function(){
        $(".box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
    });
    </script>
    </body>
</html>
Atif
  • 10,623
  • 20
  • 63
  • 96
2

change your selector to div#box and it'll work like so

$(document).ready(function(){
    $("div#box").mouseenter(function(){ $(this).css("border", "1px gray outset");}).mouseleave(function(){ $(this).css("border", "none");});
});

Also note that it's bad practice to have multiple elements with the same id. ​

DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
2

The syntax $('#box') means go and select the first element whose id matches "box". What you want to do is using class instead or if you insist on using id $('div[id="box"]') will do what you want

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206