0

I am passing a table with a button in drupal 7 and trying to print the values but iam not getting any alert

$(document).ready(function() {
  $('#edit-title-poc').change(function() {
    alert('sdsd'); //**this alert works** 
    var poc_valueasdasd = $("#edit-title-poc option").filter(":selected").val();
    $.ajax({
      url: Drupal.settings.basePath + 'ajaxpocsdas3',
      data: {
        pocdasd: poc_valueasdasd
      },
      success: function(aasdsabc) {
        //data: value returned from server
        $('#msg-diasdsadsad').html(aasdsabc);
      }
    });
  });

  $('#poc3modulepdf-generator').click(function() {
    alert("Handler for .click() called."); //this alert is not working
  });
});

This is the screen shot

enter image description here

enter image description here

t.niese
  • 39,256
  • 9
  • 74
  • 101
Madhu Nair
  • 428
  • 1
  • 7
  • 20

1 Answers1

0

Use

$(document).on('click','#poc3modulepdf-generator',function(){
    alert('clicked')
})

The reason why your code is not working is because you are binding an event to the element which is not yet created.

Using $(document).on('click','#poc3modulepdf-generator') will bind the event to document. This is event delegation. For more details on event delegation see here

I hope this will help.

Farhan Haque
  • 991
  • 1
  • 9
  • 21
  • 1
    When writing an answer you have to explain why this will work and why it won't work with the code of the OP. – t.niese Feb 26 '18 at 09:20