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 7808853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:15:13+00:00 2026-06-02T03:15:13+00:00

1 .I want to dynamically generate div containing textbox with unique id on click

  • 0

1.I want to dynamically generate div containing textbox with unique id on click of button

      <input id="<%:rid %>" type="button" value="reply"/>

2.I also want to use jquery ajax mathod to carry the textbox data to ashx file .

Can anyone help me

code

 var lineItemCount = 0;
    $(document).ready(function () {

        $(".commentbox input[type='button']").click(function () {

            var id = $(this).attr("id");

            alert(id);

           var cid = id.substring(5);
           var containerid = "container" + cid;
           alert(containerid);

            //Increase the lineitemcount
            lineItemCount++;
            //Add a new lineitem to the container, pass the lineItemCount to makesure                      
            getLineItem() 
            // can generate a unique lineItem with unique Textbox ids
            $(containerid).append(getLineItem(lineItemCount));
        });
});
 //Create a new DIV with Textboxes
     function getLineItem(number) {
         var div = document.createElement('div');
         //Give the div a unique id
           div.setAttribute('id', 'lineitem_' + number);

         //pass unique values to the getTextbox() function
         var t1 = getTextbox('txt_' + number + '_1');
         div.appendChild(t1);
         return div;
     }

     //Create a textbox, make sure the id passed to this function is unique...
     function getTextbox(id) {
         var textbox = document.createElement('input');
         textbox.setAttribute('id', id);
         textbox.setAttribute('name', id);
         return textbox;
     }

iteration through model in aspx page

<%var i=1;%>
 <%foreach (var commentitem in item.commentsModelList)
  { 
    <table border="0"  class="commentbox">
    <tr>
      <%var rid = "reply" + i;%>
       <div id="<%:containerid %>">
        <td>  <input id="<%:rid %>" type="button" value="reply"/>
       </div>
    </td>
   </tr>
  </table>
  <% i++;}%>
  • 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-02T03:15:14+00:00Added an answer on June 2, 2026 at 3:15 am

    I changed your markup little bit to get the corresponding id of items on my click events

    HTML

     <table border="0"  class="commentbox">
        <tr>
            <td>Some Item text
            </td>
         </tr>
         <tr>
             <td>
                 <div id="container-1" ></div>
                 <input type="button" class='btnReply' id="reply-1" value="Reply" />             
             </td>
         </tr>
    </table>
    

    And the Script

     $(function(){     
          $(".commentbox .btnReply").click(function(){
            $(this).hide();
            var id=$(this).attr("id").split("-")[1]        
            var strDiv="<input type='text' class='txtCmnt' id='txtReply-"+id+"' /> <input type='button' class='btnSave' value='Save' id='btnSave-"+id+"' /> ";
            $("#container-"+id).html(strDiv);          
          });
    
           $(".commentbox").on("click",".btnSave",function(){
              var itemId=$(this).attr("id").split("-")[1]         
               var txt=$(this).parent().find(".txtCmnt").val();
               $.post("/echo/json/", {reply: txt, id: itemId},function(data){                            
                   alert(data);
                   //do whatever with the response
               })           
           });       
        });
    

    Here is the jsfiddle example : http://jsfiddle.net/UGMkq/30/

    You need to change the post target url to your relevant page which handles the ajax response.

    EDIT : As per the comment about handing Multiple Divs

    As long as you have the container div ids unique, it will work, I just changed the markup to include more than one item.

     <table border="0"  class="commentbox">
        <tr>
            <td>Some Item text<br/>                
                 <div id="container-1" ></div>
                 <input type="button" class='btnReply' id="reply-1" value="Reply" />             
             </td>
         </tr>
          <tr>
            <td>Some Another Content here <br/>               
                 <div id="container-2" ></div>
                 <input type="button" class='btnReply' id="reply-2" value="Reply" />             
             </td>
         </tr>
    </table>
    

    Here is the sample :http://jsfiddle.net/UGMkq/44/

    For the above output to be rendered, you probably want to write your razor syntax like this

    <table border="0"  class="commentbox">
    @foreach (var commentitem in item.commentsModelList)
    {
       <tr>
          <td>Some Another Content here<br/>
              <div id="container-@(commentitem.Id)" ></div>
              <input type="button" class='btnReply' id="reply-@(commentitem.Id)" value="Reply" />             
           </td>
        </tr>
    }
    </table>
    

    Instead of creating a new table for each item, I created a new row in existing table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting out with jQuery and want to use $(document).ready() to dynamically generate
I want to generate tooltip based on a dynamically changing background image in css.
Suppose there is a webpage with dynamically generated content -- say a div containing
I have the following HTML scenario: <div> <input id=txt0 type=text /><input type=checkbox id=chk0 /></div>
I want to dynamically generate 8 new paragraphs with jQuery: http://jsfiddle.net/johnhoffman/Dfydn/ However, this snippet
I have written a javascript to generate textbox dynamically. Code: function addtextbox(val) { var
<div id=termSheetPopup> <input type=checkbox name=SummaryInformation>Summary Information<br /> <input type=checkbox name=SwapLegs>Swap Legs<br /> <input type=checkbox
What I want to reach, is to generate dynamically text boxes with jQuery, so,
I want to achieve this $(div1 input).attr('id'); where div1 is dynamically generated through php
I want to toggleClass of lists dynamically generated with jQuery $.ajax({...}) with an XML

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.