Search code examples
javascriptprototypejs

Click each button with the same ID


I have two buttons with the same ID:

<button type="submit" onclick="//do something" id="theID">button 1</button>

<button type="submit" onclick="//do something" id="theID">button 2</button>

I would like to click both the buttons using prototype. So far I've tried the following but it doesn't work.

$('theID').each(function(item) { 
   item.click();
});

How can I easily click both buttons using prototype?


Solution

  • I have two buttons with the same ID

    There's the problem. Use classes instead of IDs; by design and definition IDs must be unique. Stuff just plain won't work if they aren't.

    $('.clickable').each(function(item) { 
       item.click();
    });
    

    And

    <button type="submit" class="clickable">button 1</button>
    
    <button type="submit" class="clickable">button 2</button>