Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For each method on textboxes that are dynamicly added

Posted on 2013-02-05
7
Medium Priority
?
134 Views
Last Modified: 2013-02-20
Hello all,

I have a live click function where if a button is clicked it adds a set of text boxes.

$(document).ready(function () {

    var counter = 2;

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



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

        newTextBoxDiv.html(' <label class="alignme" for="repdist' + counter + '">Rep' + counter + ' - </label><label for="repdist' + counter + '">Distance:</label>' +
    ' <input  onkeypress="return IsNumber(event)"  name="repdist' + counter + '" type="text" id="repsit' + counter + '" style="width:30px;" />' +
   '  <select name="kmormile' + counter + '" id="kmormile' + counter + '"> ' +
     	'<option value="Miles">Miles</option>' +
        	'<option value="Km">Km</option>' +
           ' </select>' +
                      ' <label for="repmin' + counter + '">Min:</label>' +
                   '  <input onkeypress="return IsNumber(event)"  name="repmin' + counter + '" type="text" value="00" maxlength="2" id="repmin' + counter + '" style="width:30px;" />' +

                      ' <label for="repsec' + counter + '">Sec:</label>' +
                     '<input onkeypress="return IsNumber(event)"  name="repsec' + counter + '" type="text" value="00" maxlength="2" id="repsec' + counter + '" style="width:30px;" /><a href="#" style="border: 0;"  id="addButton" class="addButton"><img style="border: 0;"  src="images/add.PNG" /></a><a href="#" id="removeButton" class="removeButton"><img style="border: 0;"  src="images/close_square.PNG" /></a>');

        newTextBoxDiv.appendTo("#TextBoxesGroup");


        counter++;
    });

Open in new window


the issue is when i go to add it to my sql how would i loop through the text boxes to add them into the db?
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
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:JamieTownsend
ID: 38854588
Firstly, jQuery live is deprecated. You should be using 'on' now.  http://api.jquery.com/on/

For the saving and to learn how it is working, I would suggest downloading a program like Fiddler and look at what is sent to the server when you finally submit your form.  This should then show you what you will have access to in "Request.Form"
0
 

Author Comment

by:runnerjp2005
ID: 38854680
Thanks i have updated live to on but i need to loop through all available fields via asp.net
0
 

Author Comment

by:runnerjp2005
ID: 38854683
is there away to count how many i have made?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 38

Accepted Solution

by:
Tom Beck earned 2000 total points
ID: 38854684
Something like this:
function loopTextBoxes(){
	var values = '';
	$('#TextBoxesGroup div[id^="TextBoxDiv"]').each(function(){
		$(this).find('input').each(function(){
			values += $(this).attr('id') + ': ' + $(this).val() + '\r\n';
		});
		values += $(this).find('select').attr('id') + ': ' + $(this).find('option:selected').val() + '\r\n\r\n';
	});
	alert(values); //Replace with code to transfer values to POST
}

Open in new window


EDIT: added the select option
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38854742
Looping through the entered values via asp.net will be a separate operation. The textboxes you are creating in your script are not server controls so you cannot access them directly from asp.net code behind. First, collect the values in a string via client side script in this format "inputId:value,inputID:value" as I have demonstrated (replace '\r\n' with commas). Assign the string to an asp:HiddenField value. On submit, you can get the hidden field value and with server side code, split the string on the comma to create an array, loop through the array, split the pair on the colon and write the two values to the database.
0
 

Author Comment

by:runnerjp2005
ID: 38854806
Hello,

I tried this
        $(".Submit").on("click", function () {

        var values = '';
        $('#TextBoxesGroup div[id^="TextBoxDiv"]').each(function () {
            $(this).find('input').each(function () {
                values += $(this).attr('id') + ': ' + $(this).val() + '\r\n';
            });
            values += $(this).find('select').attr('id') + ': ' + $(this).find('option:selected').val() + '\r\n\r\n';
        });
        alert(values); //Replace with code to transfer values to POST
    });

Open in new window


But it does not show when click submit button

<asp:Button class="classname" ID="Submit" runat="server" Text="Submit"
                                 Height="30px" Width="90px"   />
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38854906
Your click event handler is assigned to a button with class="Submit" which you do not have.

You can do the following assuming asp.net does not change the asp:button client id from the server ID you have assigned to it. In certain circumstances, asp.net prepends additional words and symbols to the server IDs you assign in order to create new client ids. This keeps all ids on the rendered page unique.

$("#Submit").on("click", function () {

Alternately, you can put the values collector script in a normal javascript function as I have done and in your asp:Button add the attribute OnClientClick="loopTextBoxes".
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

688 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