質問

When I double click the card the dialog pops up, and it is then possible to create comments. So far so good. When creating the comments it is possible to delete it.

The issue is, that the timestamps can't be removed. The way I'm trying to remove the timestamps is by this line: $('.labelStyle').remove(); I want to be able to remove the timestamps, like the others elements but how?

Live Demo

JQuery: "click" handler

$('#divComments').on('click', '.delete', function (e) {
        var uniqueval = $(this).attr("for")
        var NameOfDataValue = $('label[for=' + uniqueval + ']').text();
        $('img[for=' + uniqueval + ']').remove();
        $('label[for=' + uniqueval + ']').remove();
        $('p[for=' + uniqueval + ']').remove();
        $('.labelStyle').remove();
        var arr = $('#divComments').data('comments');
        var theIndex = -1;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].commentString== NameOfDataValue) {
                theIndex = i;
                break;
            }
        }
        if (theIndex == -1) {
            alert("Error");
        }
        else {
            $('#divComments').data("comments").splice(theIndex, 1);
        }

    });

JQuery: Add comment function

function addComment(commentString) {

        var container = $('#divComments');
        var inputs = container.find('label');
        var id = inputs.length + 1;
        var data1 = {            
            commentString: commentString
        };

        var div = $('<div />', { class: 'CommentStyle' });

        $('<label />', {
            id: 'comment' + id,
            for: 'comment' + id,
            text: commentString
        }).on('change', function () {            
            data1.commentString = $(this).text();
        }).appendTo(div);      

        $('<br/>').appendTo(div);

        var $Image = $('<img />',
            {
                "src": "/Pages/Images/alert.png",
                "class": "CommentImage",
                "for": "comment" + id
            }).appendTo(container);

        var d = new Date();
        var $fulaDate = $('<div>' + d.getDate()
            + "-" + monthNames[d.getMonth()]
            + "-" + d.getFullYear()
            + "//" + d.getHours()
            + ":" + d.getMinutes()
            + '</div>').addClass('labelStyle').append(' ~').appendTo(div);

        var $edit = $('<p />', {
            class: 'edit',
            for: 'comment' + id,
            text: 'Edit'
        }).append(' ~').appendTo(div);

        var $delete = $('<p />', {
            class: 'delete',
            for: 'comment' + id,
            text: 'Delete'
        }).appendTo(div);        

        div.appendTo(container).focus();

        container.data('comments').push(data1);

    }
役に立ちましたか?

解決

You could do:

$(this).parent().find('.labelStyle').remove();

This will select the parent of the clicked button (.CommentStyle) then find the .labelStyle and remove it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top