0

I am trying to achieve something when i click the button. The button is loaded from the server using an AJAX call. However, nothing happens when the button is clicked. Here is my code:

(This ajax call is in JS Fiddle -- only for testing purposes) JS Fiddle

Code:

<div id="target"></div>

new Request.HTML({
    url: '/echo/html/',
    data: {
        html: "<div id='0001'>"+"<h5 class='title'>Hello World</h5>"+"<h4 class='date'>2014-07-19</h4>"+"<button> Add to Calendar </button>"+"</div>",
        delay: 0
    },
    method: 'post',
    update: 'target'
}).send();

$("button").click(function(){
    var value = $(this).siblings(".title").text();
    value += $(this).siblings(".date").text();
    alert(value);
});
stud91
  • 1,854
  • 6
  • 31
  • 56

5 Answers5

1

Since the button does not yet exist you can use jQuery.on to delegate an event handler.

$("body").on('click', 'button', function(){
  var value = $(this).siblings(".title").text();
  value += $(this).siblings(".date").text();
  alert(value); 
});

Also your fiddle does not work since you are using both jQuery and MooTools but only loading MooTools.


Added after seeing authors page:

The var value = $(this).siblings(".title").text(); selector will not work. I would recommend adding an event class (as in calendar event) to the wrapper and using:

var value = $(this).parents(".event").find(".title").text();
max
  • 96,212
  • 14
  • 104
  • 165