Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Collect input elements as one string

Posted on 2014-09-24
2
Medium Priority
?
123 Views
Last Modified: 2014-09-25
Trying to collect all form elements and take all the values into one string.  In short, I am trying to validate all inpput options in one fail swoop.  Looking for a particular string within the one big blob of text.

var elements = document.forms["myForm"].getElementsByTagName("input");
	allList = elements.join();
	alert(allList);

Open in new window

0
Comment
Question by:mopar003
2 Comments
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 2000 total points
ID: 40343332
Are you using JQuery? If so you could easily do this with a call to serialize function
e.g. var s = $('#yourFormName').serialize();

See here for API and example: http://api.jquery.com/serialize/

if you need pure javascript you can do it like this:
var elements = document.getElementById('myForm').getElementsByTagName('input');
var allList = '';
for (var i=0; i<elements.length; i++ )
{
    allList += elements[i].value;
}
	alert(allList);

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 40343673
you may use :
var elements = document.forms["myForm"].getElementsByTagName("input");
var fields = [];
for(var i=0;i<elements.length;i++) {
        if( elements[i].name ) { // check if it have a name... or maybe in your case a valid ID
             var field = elements[i].name + "=" + encodeURIComponent( elements[i].value );
             fields.push(field);
        }
}
fields = fields.join();
alert(fields);

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
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

773 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