-1

i have this button inside a popup.(in a javascript file)

'<div class="thisdiv" >'+
    '<button class="btn btn-primary" >this</button>'+   
 '</div>'+

and i am accessing this in function

$('.thisdiv').on('click', function (e) {
       $('.overlay').hide();

        $('body').removeClass('overflow');

    });

this is basically supposed to be a button which, when clicked, closes the pop up. (the simple cross button) but it does not work. it does not go into the function, i was told to use delegates. but shouldn't this be the simpler method? what am i missing?

Shahsays
  • 421
  • 1
  • 7
  • 25
  • are using jquery UI dialog box? – guradio Nov 03 '15 at 07:05
  • No, its associated to a 'more info button', when that button is clicked, an overlay appears, i want to add a button on that overlay. which when clicked, closes the over lay. – Shahsays Nov 03 '15 at 07:06
  • @Faizan, try `$(".thisdiv button").on("click")`. Your current function binds event to div and not children. Also I'd suggest to give a unique ID to button and do `$("#btnID").on("click")`. This will prevent the case when you add another button in same div and this function gets called on it. – Rajesh Nov 03 '15 at 07:07
  • thankyou for the tip, however the answer below the trick. – Shahsays Nov 03 '15 at 07:08

1 Answers1

1

Seems like you are creating the elements dynamically. Then you need to use event delegation,

$(document).on('click', '.thisdiv', function (e) {
    $('.overlay').hide();
    $('body').removeClass('overflow');
});
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53