0

This is a system where user can comment on any of the post and can edit the comment also. I am making the text editable when user click on edit button. I just need guidance to use this.id in my code that how should I use that so it work perfectly. Provide some guidance here. I am trying to make a edit comment system where edit button is enabled only if the comment is posted by the user who is logged in i.e. I can not edit other's comment and which is obvious.

Below is the javascript code

$(document).ready(function () {
    var post_id = $("#hid_post_id").val();
    var user_id = $("#user_id").val();
    $.ajax({
        url :"<?php echo site_url('home/commentsByPostId');?>" ,
        type : "POST" ,
        data : { post_id : post_id} ,
        success : function(data)
                {
                    var obj = JSON.parse(data);
                    console.log(obj);
                    var output = "";
                    var edit_btn ="";

                    $.each(obj , function(index , value)
                    {
                        output += '<div class="comnt"><p id="cmnt_txt'+i+'">"<i>'+value.cmnt_text+'"</i></p><br /><p><strong>Commented by : </strong><i>'+value.usr_name+'</i>&nbsp;&nbsp;<i>on</i> &nbsp;&nbsp;'+value.cmnt_date_time+'</p>';
                        if(value.cmnt_by_usr_id == user_id)
                        {
                            output+= '<button onclick="abc('+this.id+')">EDIT</button>'; // Here I need to pass the id "cmnt_txt which is concatenated by i to make it unique for each"
                        }
                        output += '</div>';
                    i++;
                    });         
                    $('#output_comment_box').html(output);
                }
});

/*Below is the function abc

function abc(id) // Here I am making the comment editable
       {
            var n_id = id;
            var $div=$('#n_id'), isEditable=$div.is('.editable');
                 $('#n_id').prop('contenteditable',!isEditable).toggleClass('editable')
            }

Chanchal S
  • 29
  • 4

0 Answers0