Solved

Collect input elements as one string

Posted on 2014-09-24
2
100 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now