?
Solved

Dynamically add textbox using jquery

Posted on 2013-01-22
6
Medium Priority
?
667 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 2000 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…
Suggested Courses

777 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