1

I use ajax to call PHP to replace two lines in my HTML file. The original lines still have the expected jquery click function, but the click function does not appear on the new two lines echo'ed by my php file (I made sure the class name is exactly the same).

I look up on the Event listener on Chrome inspect element feature and yep, the .click function is missing in the new lines.

Here are my codes:

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="replacable">
    <p class="editable" id="line1">Edit line 1</p>  
    <p class="editable" id="line2">Edit line 2</p>
</div>
<h4 id="click_to_replace">Click to replace line 1 and 2</h4>

<script>
    $('.editable').click(function(){
        alert('ID =  ' + $(this).attr('id'));
    });
    $('#click_to_replace').click(function(){
        $.ajax({
            type: 'POST',
            url: "replace.php",
            data: {id: $(this).attr('id')},
            success: function(newp){
                $('#replacable').replaceWith(newp);
            }
        });
    });             
</script>

The php file:

<?php
 echo '<div id="replacable">
       <p class="editable" id="line3">Edit line 3 NEW</p>   
       <p class="editable" id="line4">Edit line 4 NEW</p></div>'
?>
vagaryblue
  • 73
  • 1
  • 8

6 Answers6

3

Your click handler is only attached to the elements that exist when your code hooking up the handler ran.

You can use event delegation for this situation:

$(document.body).on('click', '.editable', function(){
    alert('ID =  ' + this.id); // $(this).attr('id')); - No need for attr here
});

That hooks click on document.body, but only fires the handler if the click passed through a .editable. If it did, jQuery fires the handler as though it were attached directly to the element. That way, you don't care whether the elements are created later or not.

You don't have to use document.body; just use an ancestor element that you won't be replacing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

You can use .on() to add click handler to the created element. see below code

$(document).on('click','.editable',function(){
        alert('ID =  ' + $(this).attr('id'));
    });

Above code will attache the click event to the specified selector. See this for more information of .on()

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

You need to use .on() for dynamically created elements.

  $('body').on('click', '#click_to_replace', function(){
renakre
  • 8,001
  • 5
  • 46
  • 99
0

as of JQuery 1.7 you should use .on

$(document.body).on('click', '.editable', function(){
    alert('ID =  ' + this.id);
});

if you are using jQuery prior to 1.7 then Try using live

$(".editable").live('click', function(){
    alert('ID =  ' + this.id);
});

Note: .live is deprecated.

Amy
  • 4,034
  • 1
  • 20
  • 34
0
 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="replacable">
    <p class="editable" id="line1">Edit line 1</p>  
    <p class="editable" id="line2">Edit line 2</p>
</div>
<h4 id="click_to_replace">Click to replace line 1 and 2</h4>

<script>

    $('#click_to_replace').click(function(){
        $.ajax({
            type: 'POST',
            url: "replace.php",
            data: {id: $(this).attr('id')},
            success: function(newp){
                $('#replacable').replaceWith(newp);
            }
        });
    $('.editable').click(function(){
        alert('ID =  ' + $(this).attr('id'));
    });
    });             
</script>

**Now try this one Hope This will work because your are adding some tags live so those are not present at the time of DOM loading they are formed after the ajax call**
0

You already had good answers but if you want more informations see this similar post, I had the same issue lately and it make me understood well why it couldn't work.

Event binding on dynamically created elements

Greetings

Community
  • 1
  • 1
Daniel
  • 1,172
  • 14
  • 31