Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Jquery .click event with multiple div and one ID

    Hi, I have a php script that create 3 divs with the same ID :

    <div id="test">1</div>
    <div id="test">2</div>
    <div id="test">3</div>


    And I have a jquery junction to handle the .click event :

    $("#test").click(function(event) {
    console.log(randomvar);
    });

    The problem is that the jquery funcion works great but ONLY for the FIRST DIV (<div id="test">1</div>), the jquery function is NOT doing anything with DIV 2 and DIV 3, why ?? Thanks!

  2. #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    9,864
    Thanks
    6
    Thanked 1,164 Times in 1,135 Posts
    You cannot have the same ID multiple times in the same document, an ID must be unique. If you want the same handle on multiple elements you need to use classes.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •