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

  • SEARCH
  • Home
  • 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 6800495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:58:34+00:00 2026-05-26T18:58:34+00:00

I have list of divs with some data in there: <div style=border: 1px solid

  • 0

I have list of divs with some data in there:

<div style="border: 1px solid #dddddd;">        
        <div id="wrap">
        <h3  id="cText">@Model.CommentText</h3>   
        <a id="runEdit" href="#" >Edit</a> 
        </div>
</div>

When user click on runEdit link I make edit from this:

e.preventDefault();

        var txt = $('#cText').text();

        $('#cText').remove();

        $('#wrap').prepend('<textarea>' + txt + '</textarea>');
        $('#wrap').append('<input type="submit" value="Ok" />');
        $('#wrap').append('<input type="submit" value="Cancel" />');

The problem is I added here this two buttons in javascript. But I don’t know how to attach some controller action to this buttons?

The problem here is that if I write 5 comments. And click on edit I get 5 edit forms.

$('#editButton').live('click', function (e) {
        e.preventDefault();

        var container = $(this).closest('.commentWrap');
        var itemId = container.attr('id');

        var nestId = '#' + itemId;

        var txt = $('#commentTextValue').text();

        $(nestId + ' #commentTextValue').remove();
        $(nestId + ' #editButton').remove();
        $(nestId).prepend('<textarea id="editArea">' + txt + '</textarea>');
        $(nestId).append('<input type="submit" value="Ok" class="btnOk" />');
    })


    $('.btnOk').live('click', function (e) {
        e.preventDefault();
        var container = $(this).closest('.commentWrap');
        var itemId = container.attr('id');
        var text = container.find('textarea').val();

        var nestId = '#' + itemId;
        //alert(nestId);

        $.ajax({
            url: '/Comment/SaveComment',
            data: JSON.stringify({ CommentText: text, CommentId: itemId }),
            type: 'post',
            contentType: 'application/json',
            success: function (data) {
                if (data.success === true) {
                    //alert(data.message); // do show/hide stuff here instead of the alert
                    $(nestId + ' #editArea').remove();
                    $(nestId + ' .btnOk').remove();
                    $(nestId).append('<h3 id="commentTextValue">' + data.message + '</h3>');
                    $(nestId).append('<a id="editButton" href="#">Edit</a>');

                }
            }
        });

    });


</script>

    <div style="border: 1px solid #dddddd;">
        @Html.ActionLink(@Model.Author, "SomeAction")
        <div class="commentWrap" id="@Model.CommentId">
            <p id="commentTextValue">@Model.CommentText</p>
            <a id="editButton" href="#">Edit</a>
        </div>
    </div>
  • 1 1 Answer
  • 1 View
  • 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-05-26T18:58:35+00:00Added an answer on May 26, 2026 at 6:58 pm

    First add an itemid to the div like this, and convert the id=wrap to a class, as there are more than one of them.

    <div class="wrap" id="123"></div>
    

    That way you get a way to reference the id of the item that you are editing.

    You should also add a class to the submit button that you inject on the page, fx:

    <input type="submit" class="btnOk" value="Ok"/>
    

    Then you can hook up the javascript:

    $('.btnOk').live('click',function(e){
       e.preventDefault();
       var container = $(this).closest('.wrap');
       var itemId = container.attr('id');
       var text = container.find('textarea')[0].val();
       $.ajax({
         url: '/mycontroller/savecomment',
         data: JSON.stringify({comment: text, id:itemId}), // using JSON2, see below
         type: 'post',
         contentType: 'application/json',
         success: function(data){
           if(data.success===true){
              alert(data.message); // do show/hide stuff here instead of the alert
    
            }
         }
       });
    
    }); 
    

    NOTE: Download the json2 library and add it to you script references – it’s a good way to do your json serialization. (https://github.com/douglascrockford/JSON-js)

    In your controller you must add an action method to handle the request:

        public ActionResult SaveComment(string text, int id)
        {
            //save your comment for the thing with that id
            var result = new {success = true, message = "saved ok"};
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data wrapped in 3 divs, each div represents a 'tab', and
This is my code <ul style=list-style-type: circle; margin-left:70px;> <li > <a style=> <div style=border:1px
I have a list of Divs within another div and I want to only
i have some dynamic code that will make a list of divs. i want
I have a view where ill have 3 divs: Div 1: List of Brands
I have a list of div pairs: MainDiv's and infPanel Divs. I need to
I have a list of divs with onclick actions associated with them. Inside each
I have a list which have divs inside it.. Now when i use nested
I have an containing a list of divs that jQuery turns into progress bars.
I have a list of images inside divs and a jquery ui slider, when

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.