Solved

Dynamically add textbox using jquery

Posted on 2013-01-22
6
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

726 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