Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically add a div using JQuery?
    primarykey
    data
    text
    <p>I have the following html which displays 3 textboxes and an add button:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div id="container"&gt; &lt;div id="line-item"&gt; &lt;asp:TextBox ID="txtLineNumber" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:TextBox ID="txtQty" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:TextBox ID="txtItemCode" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:ImageButton ID="imgBtnAddNewLineItem" ImageUrl="~/images/add_button.jpg" runat="server" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2 /jquery.min.js"&gt; &lt;/script&gt; &lt;script src="js/invoice.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When the user clicks the add button, I want to create another div with the line-item id and put it on the next line. I created a js file, but I am not sure how to do it?</p> <p>Here is what I have so far:</p> <p>var itemCount = 0;</p> <pre><code>function getLineItem(number) { var div = document.createElement('div'); $(div).attr("id", "lineitem" + number); var t1 = getTextbox("txt" + number.toString() + "1"); var t2 = getTextbox("txt" + number.toString() + "2"); var t3 = getTextbox("txt" + number.toString() + "3"); $(div).append(t1); $(div).append(t2); $(div).append(t3); return $(div); } function getTextbox(id) { var textbox = document.createElement('input'); $(textbox).attr("id", id); return $(textbox); } var lineItemCount = 0; $('#imgBtnAddNewLineItem').click(function() { lineItemCount++; $('#line-item').clone().attr('id',getLineItem(lineItemCount)).appendTo('#container'); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload