Solved

concatenate text input fields on submit

Posted on 2011-03-15
13
993 Views
Last Modified: 2012-05-11
I have a form that allows me to dynamically add fields. For example one field is named "part_numbers". When the renders in the browser it renames each field sequentially using this syntax: parts[parts][0][part_numbers]

so for instance, if my form has the 3 part_numbers fields it will now have:
parts[parts][0][part_numbers]
parts[parts][1][part_numbers]
parts[parts][2][part_numbers]

I need to take each of these values and concatenate them with a comma separator. How can I do that and should it be attached to the submit function?

thanks.
0
Comment
Question by:befidled
[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
  • 7
  • 5
13 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35139380
>How can I do that and should it be attached to the submit function?
Try again :)

What do you get in the alert with the following code and what don't you want or what it miss ?

$("form").submit(function() {
  part_numbers = "";
  $.map( $("form").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el.value) });
  $("#part_number").val( part_numbers );
  alert( part_numbers );
});

Open in new window

0
 
LVL 1

Author Comment

by:befidled
ID: 35139461
that gets me closer. It returns the values of all fields in my form in the alert.

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35139486
So what do you get in the alert with the following code and what don't you want or what it miss ?

This line put all this value in a field with the id "part_number" : $("#part_number").val( part_numbers );


<input name="part_number" id="part_number" type="hidden" />

Open in new window

0
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.

 
LVL 82

Expert Comment

by:leakim971
ID: 35139510
if this field did not exist you can create it on the fly before using : $("body").append("<input name=\"part_number\" id=\"part_number\" type=\"hidden\" />";





$("form").submit(function() {
  part_numbers = "";
  $.map( $("form").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el.value) });
  $("body").append("<input name=\"part_number\" id=\"part_number\" type=\"hidden\" />";
  $("#part_number").val( part_numbers );
  //alert( part_numbers ); checking off
});

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35139520
So what do you get in the alert with the following code and what don't you want or what it miss ?
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35139633

Could you use a class to identify which fields are to be concatenated, as below:

jQuery(document).ready(function(){

  $("#click-me").click( function() {
    var pick = [];
    $(this).closest("form").find(".concatenate-me").each( function() { pick.push( $(this).val() ) });
    alert( pick.toString() );
  });

});

Open in new window


<form>
  <input class="concatenate-me" value="A" />
  <input class="concatenate-me" value="B" />
  <input class="concatenate-me" value="C" />
  <input class="concatenate-me" value="Z" />
  <input id="click-me" type="button" value="Submit" />
</form>

Open in new window

0
 
LVL 1

Author Comment

by:befidled
ID: 35139718
The issue now is that it is returning every value within my form as part of "part_numbers" value:

30|/members/form-test/|open|members/thankyou-rfq||http://www.mysite.com/members/form-test/|New Form Test|freeform|17896|1|5555|NSN|Quantity||444|NSN|Quantity||||||||||||||||||05d77ad280732d5ba35e3246ddafffc2ce4bff84|3|http://www.aeronautical.com/members/form-test/|stay-informed|1|my@emailaddress.com

Open in new window


Whereas what I need is to take the part_numbers field (which in this case has values of 5555 and 444) and concatenate them together so that part_numbers = "5555, 444"

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35139794
What about :
$("form").submit(function() {
  part_numbers = "";
  $.map( $("[name^='parts[parts]']").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el.value) });
  $("#part_number").val( part_numbers );
  alert( part_numbers );
});

Open in new window

0
 
LVL 1

Author Comment

by:befidled
ID: 35139901
That's closer, however the row of fields that gets cloned actually has 4 fields each with the "parts[parts]" at the beginning of their name. The last segment of the name is [part_numbers] and is unique to each field. Is there a way to write it to fetch that?
0
 
LVL 1

Author Comment

by:befidled
ID: 35139920
i don't know if that was clear enough, here is a sample of the names of each of the four fields:

parts[parts][0][part_numbers]
parts[parts][0][nsns]
parts[parts][0][quantity_required]
parts[parts][0][condition_required]

So if we can isolate them based on the last segment of their name the above code should work.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35139949
yes using $ :


$("form").submit(function() {
  part_numbers = "";
  $.map( $("[name^='parts[parts]'][name$='[part_numbers]']").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el.value) });
  $("#part_number").val( part_numbers );
  alert( part_numbers );
});

Open in new window

0
 
LVL 1

Author Comment

by:befidled
ID: 35141098
ah perfect. Thanks so much leakim!

brian
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35141114
You're welcome! Thanks for the points!
0

Featured Post

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 60
Sticking text to shapes in Raphael.js 6 37
Javascript Error 10 20
Error just appeared 3 18
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

740 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