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

19.9k views Asked by At

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?

2

There are 2 answers

1
Resolution On BEST ANSWER

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

0
Balachandran On

use common class for all buttons

$('.commonClass').bind('click', function(e) {
            e.preventDefault();
            $('#element_to_pop_up').bPopup();
        });

DEMO