Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:55:54+00:00 2026-06-11T18:55:54+00:00

I am putting this question again by reconstructing it….as I still not have any

  • 0

I am putting this question again by reconstructing it….as I still not have any solution…

I am developing MVC 3 application and using razor syntax.

In this application I am giving commenting facility.

I have given the facility to adding a comment and it saved in DB.

and when user clicks on delete button it displays the message as “Clicked”.

When user load entity, previously added comments get displayed on page with
delete button and when user click on that button the “clicked” msg appears.

enter image description here

now, when user add a new comment, it saved in DB sucsessfully and also appear on the page
along with Delete button.

now when user click on delete button msg wontcome…
( I append the Div tag while loading the new comment from DB)

I think , there is a issue regarding append, means previous comments Delete button
work well, but when I add button using append it wont works…

Here is the code which is in partial view which add comment in DB and again fetches latest comment back to view to display It.

    <script src="../../Scripts/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $('#AddCommentButton').click(function () 

        {
        var comment1 = $('#Comment').val();
        if (comment1 == '') {
            alert('Please enter a comment');
            return;
        }


            $.ajax({

                type: 'post',
                url: '/Comment/SaveComments',
                dataType: 'json',


                data:
                { 

                 'comments' : $('#Comment').val(), 
                 'EType' : @Html.Raw(Json.Encode(ViewBag.EType)), 
                  'EId' : @Html.Raw(Json.Encode(ViewBag.EId))

                },
                success: function (data) {


                                   $("p.p12").append('<button type="button" id=' + data.Id  + ' class="deleteComment2">Delete</button></div>')



                }

            });
        });
    });
</script>

I use below code, when i clicked on the delete button…

<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

    $('.deleteComment').live('click', function () {
        alert('Clicked');
    });

</script>

Now , the summary of a problem is I append div and in that div I add a button and I am trying to get click event of that button but I cant get that event.

Can anyone please give the simple code for handling of button’s click event when button get added runtime in the Div tag.

Here is the new code, I have added your code in page, but it still not working.

<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>

@model  IEnumerable<CRMEntities.Comment>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


 <!DOCTYPE html>


<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

   $(document).on('click', '.deleteComment', function() 
   {
    alert('comment deleted');    
});


$(document).ready(function () {
alert("Doc loaded");

});
​

</script>




<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $('#AddCommentButton').click(function () {

            $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
            $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
            $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
            $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
        });
    });
</script>




<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".ShowComments").click(function () {
            $(".ParentBlock").slideToggle("slow");

        });
    });
</script>



</head>
<body>

@{





     <div class="ParentBlock">



    @foreach (var item in Model)
    {
        <div class="OwnerClass" id="OwnerName">


         <span class="EmpName"> @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>

           @Html.DisplayFor(ModelItem => item.CommentDateTime)

          <span class="EmpName"><button type="button" id = "@item.Id" class="deleteComment">Delete</button></span>



          <span class="EmpName"> @Html.ActionLink("Delete", "Delete", "Comment", new { id = item.Id }, new { @style = "color:#1A6690;" })</span>


        <p class="CommentP">
           @Html.DisplayFor(ModelItem => item.CommentText)
        </p>

        <br />
            <a class="Delete222" style="cursor:move;display:none;">DeleteNew</a>
        <br />

        </div>


    }


     <p class="p12">

      </p>



</div>

      <p id="ClassPara" class="ShowComments" onclick="chkToggle()">Show All Comments</p>

}



   @Html.TextArea("Comment", "", 5, 80, "asdsd")


    <input type="button" value="Add Comment" id="AddCommentButton"/>                         
    <input type="button" value="Clear" onclick="clearText()"/>                    

    <br />

    <div id="comments22">
    <input type="button" class="deleteComment" value="Delete" /><br/>
    <input type="button" class="deleteComment" value="Delete" /><br/>    
    <input type="button" class="deleteComment" value="Delete" /><br/>


</div>




</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:55:55+00:00Added an answer on June 11, 2026 at 6:55 pm

    How many times do you intend to include jQuery in your page? 1 should be enough. You seem to have included it in a 3 different places. Also you seem to have placed your scripts outside if the <html> element which is completely wrong.

    Only one inclusion is necessary:

    <script src="@Url.Content("~/Scripts/jquery-1.8.0.min.js")" type="text/javascript"></script>
    

    Also you should use the .on() method to subscribe to the click event of the delete button in a lively manner:

    $(document).on('click', '.deleteComment', function () {
        alert('Clicked');
    });
    

    And last but not least you seem to be using the runat="server" attribute on your <head> element. That’s Razor, not WebForms – forget about those attributes.

    So let me clean this code for you because it is a complete mess:

    @model IEnumerable<CRMEntities.Comment>
    @{ 
        Layout = null;
    }
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
    <body>
        <div class="ParentBlock">
            @foreach (var item in Model)
            {
                <div class="OwnerClass" id="OwnerName">
                    <span class="EmpName"> 
                        @Html.ActionLink(
                            item.Owner.FullName, 
                            "Details", 
                            "EMployee", 
                            new { id = item.OwnerId }, 
                            new { @style = "color:#1A6690;" }
                        )
                    </span>
    
                    @Html.DisplayFor(ModelItem => item.CommentDateTime)
    
                    <span class="EmpName">
                        <button type="button" id = "@item.Id" class="deleteComment">Delete</button>
                    </span>
    
                    <span class="EmpName"> 
                        @Html.ActionLink(
                            "Delete", 
                            "Delete", 
                            "Comment", 
                            new { id = item.Id }, 
                            new { @style = "color:#1A6690;" }
                        )
                    </span>
    
                    <p class="CommentP">
                       @Html.DisplayFor(ModelItem => item.CommentText)
                    </p>
    
                    <br />
                    <a class="Delete222" style="cursor:move;display:none;">DeleteNew</a>
                    <br />
                </div>
            }
            <p class="p12"></p>
        </div>
    
        <p id="ClassPara" class="ShowComments" onclick="chkToggle()">Show All Comments</p>
    
        @Html.TextArea("Comment", "", 5, 80, "asdsd")
    
        <input type="button" value="Add Comment" id="AddCommentButton"/>                         
        <input type="button" value="Clear" onclick="clearText()"/>                    
    
        <br />
    
        <div id="comments22">
            <input type="button" class="deleteComment" value="Delete" /><br/>
            <input type="button" class="deleteComment" value="Delete" /><br/>    
            <input type="button" class="deleteComment" value="Delete" /><br/>
        </div>
    
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
        <!-- or use your own jQuery version locally: -->
        @*<script type="text/javascript" src="@Url.Content("~/scripts/jquery-1.8.1.js")"></script>*@
        <script type="text/javascript">
            $(document).on('click', '.deleteComment', function () {
                alert('comment deleted');
            });
    
            $('.ShowComments').click(function () {
                $('.ParentBlock').slideToggle('slow');
            });
    
            $('#AddCommentButton').click(function () {
                $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
                $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
                $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
                $('#comments22').append('<input type="button" class="deleteComment" value="Delete" /><br/>');
            });
        </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been putting off developing this part of my app for sometime purely
I have just started using RadControls so this question might be basic for you,
This question may seem blindingly obvious and I realise I am putting myself up
I have seen this same question in many forms on this site and in
This is probably a simple question, but it's driving me crazy! I have a
Note: Move this question to other site if you think its not suite here
I'm not too sure how to ask this question. I need to store a
I have a tricky question and I'm not sure if it is even possible
Does it make sense to use an OR-mapper? I am putting this question of
This question has two aspects, both related to indices. I have a dataset with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.