tags:

views:

6574

answers:

3

I have the following html which displays 3 textboxes and an add button:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container">
    <div id="line-item">
    <asp:TextBox ID="txtLineNumber" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtItemCode" runat="server"></asp:TextBox>
    <asp:ImageButton ID="imgBtnAddNewLineItem" ImageUrl="~/images/add_button.jpg" 
    runat="server" />
   </div>
   </div>
   </form>

   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2
   /jquery.min.js">
   </script>
   <script src="js/invoice.js" type="text/javascript"></script>
</body>
</html>

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?

Here is what I have so far:

var itemCount = 0;

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');
    });
});
+4  A: 
$(document).ready(function() {
    $('#imgBtnAddNewLineItem').click(function() {
        $('#container').append('<div></div>');
    });
});

Update 2

Create a normal button like so:

<input type="button" id="imgBtnAddNewLineItem" value="Add lineitem" />

Update ** This is also updated with comments etc.. **

//Count the lineItems to make sure they are unique
var lineItemCount = 0;

//On document ready 
$(document).ready(function() {
    //On button click
    $('#imgBtnAddNewLineItem').click(function(e) {
        /*
           ADD THE FOLLOWING LINE TO PREVENT THE POSTBACK
           P.S. - Make sure you pass -e- to this function... 

         */
         e.preventDefault();



         //Increase the lineitemcount
         lineItemCount++;
         //Add a new lineitem to the container, pass the lineItemCount to make sure getLineItem() can generate a unique lineItem with unique Textbox ids
         $(container).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');
    var t2 = getTextbox('txt_' + number + '_2');
    var t3 = getTextbox('txt_' + number + '_3');

    div.appendChild(t1);
    div.appendChild(t2);
    div.appendChild(t3);

    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;
}
Ropstah
I tried this, but what I actually want to do is append the div with the id line-item with the 3 textboxes and the image button, not just an empty div.
Xaisoft
Also, when I do a test with a <p>Hello</p>, it flashes on the screen but does not stay.
Xaisoft
This is globally how you append items. You can also append jQuery objects or Javascript HTML Element objects. You really have to create some logic to determine textbox id's. Those which appear at first are generated by the ASP.NET framework. Maybe you should consider creating them all (also the first line-item) from jQuery. This will help you determining which algorithm to use to determine formfield names/ids.
Ropstah
If I understand you correctly, are you saying to create all the asp.net elements in their own divs and hide them by default and append or show them when needed.
Xaisoft
No, i'm updating the answer, one sec
Ropstah
I just copied your updated example and it is not working. I am not sure how to combine it with cletus's example, though?
Xaisoft
I updated my post with yours and cletus's put together, but it shows the line-item and then disappears. I wonder if this has to do with the Image Button postback.
Xaisoft
Yes this could be. See update 2. You have to create a button that doesn't make the page go back to the server. You want to stay on the clientside where you can execute javascript.
Ropstah
Sounds more than likely! if you must use an ASP Image Button then in the $('#imgBtnAddNewLineItem').click function add a return false; as the last line this should stop the postback from firing.
OneSHOT
Great! This works. One question. When I created some new rows and viewed the html, it does not show the other created rows.
Xaisoft
No, you only see the original html returned by the server. All changes to the DOM made by javascript cannot just be viewed by opening the source. There are DOM inspectors which can though...
Ropstah
+1  A: 

You can use .append() method or .html()

Amr ElGarhy
What happens when to the asp.net ID's when I keep adding items?
Xaisoft
you will need to put the element ids as variables
Amr ElGarhy
What exactly do you mean?
Xaisoft
FYI: You can't create <asp: controls on the client side. You will have use <input tags and use Request.Form to read the data from Form_Load in the code behind.
jrcs3
jrcs3: I was actually thinking of this too, it is actually possible?
Xaisoft
i deleted the example for now till i rewrite as you are right, can't create server side elements on client side.
Amr ElGarhy
both other answers are enough
Amr ElGarhy
+4  A: 

Try something like this:

$(document).ready(function() {
  $('#imgBtnAddNewLineItem').click(function() {
    var container = $("#container");
    var line = $('#line-item').clone().attr("id", "line-item-2")
    var lineCount = container.children().length + 1;
    line.attr("id", line.attr("id") + "-" + lineCount);
    line.find(":text").each(function() {
      // IDs need to be unique
      $(this).attr("id", $(this).attr("id") + "-" + lineCount);
      $(this).attr("name", $(this).attr("name") + "-" + lineCount);
      // clear the value since we're cloning a line that may have values in it
      $(this).val("");
    });
    container.append(line);
  });
});

Note: you need to change the id.

cletus
i liked this way better than mine and others, simple, and clear
Amr ElGarhy
cletus is a good chainer.. Try to combine this with my example and you're ready to go ;)
Ropstah
cletus, that works, but the line-item shows on the screen and then disappears. Everytime I click the button, it also inserts a , in all of the textboxes.
Xaisoft
Xaisoft, add the e.preventDefault() to the click function
SteD