Solved

Collect input elements as one string

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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

617 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