0

this is my html code

    <input type="hidden" value="<?php echo $rslt['id'] ?>" id="res_id" />                            <!-- this line catch post id -->

<div id="likeSec">
    <h4><?php echo $rslt['likes'] ?> People Like this</h4>                                       <!-- this line show total likes -->

    <?php
    $kol=mysql_query("SELECT * FROM tbl_like_recipe WHERE res_id='".$rslt['id']."' && user_id='".$_SESSION['member_email']."'"); 
    $num_rows = mysql_num_rows($kol);                               // this query check this user already like this post or not    
        if($num_rows==0){                                           // if not liked already show like image
    ?>
    <div id="like" style="float:right;"><img src="images/like.png" width="45" /></div>
    <?php 
        }else{                                                      // if already like show unlike image
    ?>
    <div id="dislike" style="float:right;"><img src="images/unlike.png" width="45" /></div>
    <?php } ?>
</div>

and this my script

<script>
$(document).ready(function(){
    $("#like").click(function(){
        var res_id=$("#res_id").val();
        $.post("like_recipe_prosses.php",{'data':res_id},function(cbd){
            $("#likeSec *").remove();
            $("#likeSec").html(cbd)
        })
    })

    $("#dislike").click(function(){
        var res_id=$("#res_id").val();
        $.post("dislike_recipe_prosses.php",{'data':res_id},function(cbd){
            $("#likeSec *").remove();
            $("#likeSec").html(cbd)
        })
    })
})
</script>

When I click 'like' I see it becomes 'Unlike', but if I click on 'Unlike' it doesn't work. If I then refresh the page and click 'Unlike', it becomes 'Like', but I can not click 'Like' again to make it 'Unlike' again.

What did I do wrong here? please help me someone.

2
  • do you get any error in you console ? Commented Jan 21, 2014 at 18:50
  • $("#likeSec *").remove(); is redundant Commented Jan 21, 2014 at 18:56

3 Answers 3

3

You are generating dynamic elements in your DOM, thus the click event wont be attached due to the lack of delegation, in order to make this work, try the following:

$("#likeSec").delegate('#like', 'click', function(){
    var res_id=$("#res_id").val();
    $.post("like_recipe_prosses.php",{'data':res_id},function(cbd){
        $("#likeSec *").remove();
        $("#likeSec").html(cbd)
    })
})

$("#likeSec").delegate('#dislike', 'click', function(){
    var res_id=$("#res_id").val();
    $.post("dislike_recipe_prosses.php",{'data':res_id},function(cbd){
        $("#likeSec *").remove();
        $("#likeSec").html(cbd)
    })
})
Sign up to request clarification or add additional context in comments.

1 Comment

From jquery docs: As of jQuery 1.7, .delegate() has been superseded by the .on() method.. See example with .on() below
1

Try this

$('#likeSec').on('click','yourdiv',function(){
   //Your Code
 });

1 Comment

From jquery docs: As of jQuery 1.7, .delegate() has been superseded by the .on() method.
1

When you receive ajax response, you replace original HTML elements which had events attached with new HTML. Use .on to attach delegated events:

$("#likeSec").on("click", "#like", function(){
    var res_id=$("#res_id").val();
    $.post("like_recipe_prosses.php",{'data':res_id},function(cbd){
        $("#likeSec").html(cbd)
    })
})

$("#likeSec").on("click", "#dislike", function(){
    var res_id=$("#res_id").val();
    $.post("dislike_recipe_prosses.php",{'data':res_id},function(cbd){
        $("#likeSec").html(cbd)
    })
})

1 Comment

From jquery docs: As of jQuery 1.7, .delegate() has been superseded by the .on() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.