最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple buttons with the same #id to do the same function? - Stack Overflow

programmeradmin1浏览0评论

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);

/ - 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?

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?

Share Improve this question edited Dec 12, 2014 at 9:44 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Dec 12, 2014 at 9:41 commoncommon 1011 gold badge2 silver badges9 bronze badges 2
  • 3 You should use classes when you want to identify multiple elements. id should be unique – Hacketo Commented Dec 12, 2014 at 9:43
  • 3 By definition, an id must be unique – pistou Commented Dec 12, 2014 at 9:44
Add a comment  | 

2 Answers 2

Reset to default 12

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

use common class for all buttons

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

DEMO

发布评论

评论列表(0)

  1. 暂无评论