Solved

Dynamically add textbox using jquery

Posted on 2013-01-22
6
654 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
Comment Utility
0
 

Author Comment

by:runnerjp2005
Comment Utility
will  newTextBoxDiv.html accept  <asp: ?
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
not sure what do you mean...

Do you mean will it work with ASP.NET? I do not see why not
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
JQuery from the link to my html page and it works great

how did you do this>
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now