Solved

Collect input elements as one string

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

Industry Leaders: 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

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…
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…

734 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