Solved

Dynamically add textbox using jquery

Posted on 2013-01-22
6
656 Views
Last Modified: 2013-01-23
How can I dynamically add the textbox's if add is selected?

<div id='TextBoxesGroup'>
                        <asp:Literal id="Rep" Text="Rep-"   runat="server" />
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<asp:Literal id="Literal4" Text="Distance:"   runat="server" />
                       &nbsp;<asp:TextBox ID="TextBox4" Width="30" runat="server"></asp:TextBox>
                        <asp:Literal id="min6" Text="Min:"   runat="server" /> 
                     <asp:TextBox ID="min7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox>
                        <asp:Literal id="sec6" Text="Secs"   runat="server" /> 
                     <asp:TextBox ID="sec7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox><a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a>
                      </div>

Open in new window


Thanks
0
Comment
Question by:runnerjp2005
  • 4
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 38805978
0
 

Author Comment

by:runnerjp2005
ID: 38805982
will  newTextBoxDiv.html accept  <asp: ?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38806002
not sure what do you mean...

Do you mean will it work with ASP.NET? I do not see why not
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Expert Comment

by:YZlat
ID: 38806011
just tested it and it worked great for me.

I just save your div to an .html file
<div id='TextBoxesGroup'>
                        <asp:Literal id="Rep" Text="Rep-"   runat="server" />
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<asp:Literal id="Literal4" Text="Distance:"   runat="server" />
                       &nbsp;<asp:TextBox ID="TextBox4" Width="30" runat="server"></asp:TextBox>
                        <asp:Literal id="min6" Text="Min:"   runat="server" />
                     <asp:TextBox ID="min7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox>
                        <asp:Literal id="sec6" Text="Secs"   runat="server" />
                     <asp:TextBox ID="sec7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox><a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a>
                      </div>

and added the JQuery from the link to my html page and it works great
0
 

Author Comment

by:runnerjp2005
ID: 38806021
JQuery from the link to my html page and it works great

how did you do this>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38806033
save this as an html page and see for yourself. Just make sure that in the folder where you save the html file you have a directory called js where you need to put the latest jquery file names jquery.js

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

$(document).ready( function(){
	var counter = 2;

    $(".addButton").click(function () {

 if(counter>5){
            alert("Only 5 textboxes allow");
            return false;
 }   

 var newTextBoxDiv = $(document.createElement('div'))
      .attr("id", 'TextBoxDiv' + counter);

 newTextBoxDiv.html('<TABLE><TR><TD>' +
'<input type="text" name="textbox' + counter + 
'" id="textbox' + counter + '" value="" ></TD><TD><input type="text" name="textbox' + counter + 
'" id="textbox' + counter + '" value="" ></TD>&nbsp;<TD><a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a></TD></TR></TABLE>');

 newTextBoxDiv.appendTo("#TextBoxesGroup");


 counter++;
     });

     $(".removeButton").click(function () {
 if(counter==1){
          alert("No more textbox to remove");
          return false;
       }   

 counter--;

        $("#TextBoxDiv" + counter).remove();

     });

     $("#getButtonValue").click(function () {

 var msg = '';
 for(i=1; i<counter; i++){
      msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
 }
       alert(msg);
     });

});

</script>
</head>
<body>

 <div id='TextBoxesGroup'>
                        <asp:Literal id="Rep" Text="Rep-"   runat="server" />
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<asp:Literal id="Literal4" Text="Distance:"   runat="server" />
                       &nbsp;<asp:TextBox ID="TextBox4" Width="30" runat="server"></asp:TextBox>
                        <asp:Literal id="min6" Text="Min:"   runat="server" /> 
                     <asp:TextBox ID="min7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox>
                        <asp:Literal id="sec6" Text="Secs"   runat="server" /> 
                     <asp:TextBox ID="sec7" Text="00" Width="30" MaxLength="2" runat="server"></asp:TextBox><a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a>
                      </div> 
</body>
</HTML>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
imap mails 1 22
JqueryUI sortable append variable to post 1 38
Jquery 10 20
How do I pull the base url for use in html links 7 23
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question