Search code examples
javascriptjquerybuttonbpopup

Multiple buttons with the same #id to do the same function?


JS beginner, sorry

How can could I make it so that every button that has the id "#popit" to open the same popup box?

I'm using bPopup

With this code there is only one button on the site which does open the popup

;(function($) {
    $(function() {
        $('#my-button').bind('click', function(e) {
            e.preventDefault();
            $('#element_to_pop_up').bPopup();
        });
    });
})(jQuery);

http://jsfiddle.net/yg5so25s/ - there are 3 buttons with the same id, but only the first one opens the popup box, anyway I could make it so that every single button to open the same popupbox?


Solution

  • id must be unique, you need to use class instead:

    <button class="my-button">POP IT UP</button>
    

    then you can use . to target elements by class name:

    ;(function($) {
        $(function() {
            $('.my-button').bind('click', function(e) {
                e.preventDefault();
                $('#element_to_pop_up').bPopup();
            });
        });
    })(jQuery);
    

    Updated Fiddle