Solved

Collect input elements as one string

Posted on 2014-09-24
2
105 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 500 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 82

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i select 4 32
maps stopped work unsure why 7 34
"Pause" execution of javascript function for x miliseconds 2 26
Getting Random number with min and max value. 2 28
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

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