0

Within my ajax success function, I have the following code:

$('#searchButton').on('click',function(){
//ajax call
//within that ajax call's success function:
$.ajax({
      type: 'POST',
      url: 'search.php',      
      data: {x:x},        
      success:function(dataReturn){              

        $('#returnThree').html(
        "<img id='expandThree' class='expandThree' src='../../img/expand.png'>"
        );

I have another function that I want to be called when the user clicks on this image, but I can't figure out where to put the code as nothing seems to work:

$("#expandThree").click(function() {
  console.log('hi');
  });

I've tried putting it both inside and outside of the $('#searchButton').on('click',function(){, but it's not calling. What am I doing wrong?

Brian Powell
  • 3,336
  • 4
  • 34
  • 60

1 Answers1

1

Try this on your success callback:

var $img = $("<img id='expandThree' class='expandThree' src='../../img/expand.png'>")
               .appendTo('#returnThree')
               .on("click", function() {});

Fiddle

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105